Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * acpi_battery.c - ACPI Battery Driver ($Revision: 37 $) | |
3 | * | |
4 | * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> | |
5 | * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> | |
6 | * | |
7 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or (at | |
12 | * your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, but | |
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License along | |
20 | * with this program; if not, write to the Free Software Foundation, Inc., | |
21 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
22 | * | |
23 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
24 | */ | |
25 | ||
26 | #include <linux/kernel.h> | |
27 | #include <linux/module.h> | |
28 | #include <linux/init.h> | |
29 | #include <linux/types.h> | |
f1d4661a | 30 | #include <linux/jiffies.h> |
1da177e4 LT |
31 | #include <linux/proc_fs.h> |
32 | #include <linux/seq_file.h> | |
33 | #include <asm/uaccess.h> | |
34 | ||
35 | #include <acpi/acpi_bus.h> | |
36 | #include <acpi/acpi_drivers.h> | |
37 | ||
1da177e4 LT |
38 | #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF |
39 | ||
1da177e4 LT |
40 | #define ACPI_BATTERY_COMPONENT 0x00040000 |
41 | #define ACPI_BATTERY_CLASS "battery" | |
1da177e4 | 42 | #define ACPI_BATTERY_DEVICE_NAME "Battery" |
1da177e4 LT |
43 | #define ACPI_BATTERY_NOTIFY_STATUS 0x80 |
44 | #define ACPI_BATTERY_NOTIFY_INFO 0x81 | |
1da177e4 | 45 | |
1da177e4 | 46 | #define _COMPONENT ACPI_BATTERY_COMPONENT |
b6ce4083 | 47 | |
f52fd66d | 48 | ACPI_MODULE_NAME("battery"); |
1da177e4 | 49 | |
f52fd66d | 50 | MODULE_AUTHOR("Paul Diefenbaugh"); |
7cda93e0 | 51 | MODULE_DESCRIPTION("ACPI Battery Driver"); |
1da177e4 LT |
52 | MODULE_LICENSE("GPL"); |
53 | ||
f1d4661a AS |
54 | static unsigned int cache_time = 1000; |
55 | module_param(cache_time, uint, 0644); | |
56 | MODULE_PARM_DESC(cache_time, "cache time in milliseconds"); | |
b6ce4083 | 57 | |
3f86b832 RT |
58 | extern struct proc_dir_entry *acpi_lock_battery_dir(void); |
59 | extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir); | |
60 | ||
4be44fcd LB |
61 | static int acpi_battery_add(struct acpi_device *device); |
62 | static int acpi_battery_remove(struct acpi_device *device, int type); | |
5d9464a4 | 63 | static int acpi_battery_resume(struct acpi_device *device); |
1da177e4 | 64 | |
1ba90e3a TR |
65 | static const struct acpi_device_id battery_device_ids[] = { |
66 | {"PNP0C0A", 0}, | |
67 | {"", 0}, | |
68 | }; | |
69 | MODULE_DEVICE_TABLE(acpi, battery_device_ids); | |
70 | ||
1da177e4 | 71 | static struct acpi_driver acpi_battery_driver = { |
c2b6705b | 72 | .name = "battery", |
4be44fcd | 73 | .class = ACPI_BATTERY_CLASS, |
1ba90e3a | 74 | .ids = battery_device_ids, |
4be44fcd LB |
75 | .ops = { |
76 | .add = acpi_battery_add, | |
34c4415a | 77 | .resume = acpi_battery_resume, |
4be44fcd LB |
78 | .remove = acpi_battery_remove, |
79 | }, | |
1da177e4 LT |
80 | }; |
81 | ||
038fdea2 | 82 | enum acpi_battery_files { |
78490d82 AS |
83 | ACPI_BATTERY_INFO = 0, |
84 | ACPI_BATTERY_STATE, | |
85 | ACPI_BATTERY_ALARM, | |
86 | ACPI_BATTERY_NUMFILES, | |
87 | }; | |
88 | ||
1da177e4 | 89 | struct acpi_battery { |
038fdea2 | 90 | struct mutex lock; |
f1d4661a AS |
91 | struct acpi_device *device; |
92 | unsigned long update_time; | |
038fdea2 AS |
93 | int present_rate; |
94 | int remaining_capacity; | |
95 | int present_voltage; | |
038fdea2 AS |
96 | int design_capacity; |
97 | int last_full_capacity; | |
98 | int technology; | |
99 | int design_voltage; | |
100 | int design_capacity_warning; | |
101 | int design_capacity_low; | |
102 | int capacity_granularity_1; | |
103 | int capacity_granularity_2; | |
f1d4661a | 104 | int alarm; |
038fdea2 AS |
105 | char model_number[32]; |
106 | char serial_number[32]; | |
107 | char type[32]; | |
108 | char oem_info[32]; | |
f1d4661a AS |
109 | int state; |
110 | int power_unit; | |
038fdea2 | 111 | u8 alarm_present; |
1da177e4 LT |
112 | }; |
113 | ||
78490d82 | 114 | inline int acpi_battery_present(struct acpi_battery *battery) |
b6ce4083 | 115 | { |
78490d82 AS |
116 | return battery->device->status.battery_present; |
117 | } | |
038fdea2 | 118 | |
f1d4661a | 119 | inline char *acpi_battery_units(struct acpi_battery *battery) |
78490d82 | 120 | { |
f1d4661a | 121 | return (battery->power_unit)?"mA":"mW"; |
b6ce4083 VL |
122 | } |
123 | ||
78490d82 AS |
124 | /* -------------------------------------------------------------------------- |
125 | Battery Management | |
126 | -------------------------------------------------------------------------- */ | |
127 | ||
038fdea2 AS |
128 | struct acpi_offsets { |
129 | size_t offset; /* offset inside struct acpi_sbs_battery */ | |
130 | u8 mode; /* int or string? */ | |
131 | }; | |
b6ce4083 | 132 | |
038fdea2 AS |
133 | static struct acpi_offsets state_offsets[] = { |
134 | {offsetof(struct acpi_battery, state), 0}, | |
135 | {offsetof(struct acpi_battery, present_rate), 0}, | |
136 | {offsetof(struct acpi_battery, remaining_capacity), 0}, | |
137 | {offsetof(struct acpi_battery, present_voltage), 0}, | |
138 | }; | |
b6ce4083 | 139 | |
038fdea2 AS |
140 | static struct acpi_offsets info_offsets[] = { |
141 | {offsetof(struct acpi_battery, power_unit), 0}, | |
142 | {offsetof(struct acpi_battery, design_capacity), 0}, | |
143 | {offsetof(struct acpi_battery, last_full_capacity), 0}, | |
144 | {offsetof(struct acpi_battery, technology), 0}, | |
145 | {offsetof(struct acpi_battery, design_voltage), 0}, | |
146 | {offsetof(struct acpi_battery, design_capacity_warning), 0}, | |
147 | {offsetof(struct acpi_battery, design_capacity_low), 0}, | |
148 | {offsetof(struct acpi_battery, capacity_granularity_1), 0}, | |
149 | {offsetof(struct acpi_battery, capacity_granularity_2), 0}, | |
150 | {offsetof(struct acpi_battery, model_number), 1}, | |
151 | {offsetof(struct acpi_battery, serial_number), 1}, | |
152 | {offsetof(struct acpi_battery, type), 1}, | |
153 | {offsetof(struct acpi_battery, oem_info), 1}, | |
154 | }; | |
b6ce4083 | 155 | |
038fdea2 AS |
156 | static int extract_package(struct acpi_battery *battery, |
157 | union acpi_object *package, | |
158 | struct acpi_offsets *offsets, int num) | |
159 | { | |
160 | int i, *x; | |
161 | union acpi_object *element; | |
162 | if (package->type != ACPI_TYPE_PACKAGE) | |
163 | return -EFAULT; | |
164 | for (i = 0; i < num; ++i) { | |
165 | if (package->package.count <= i) | |
166 | return -EFAULT; | |
167 | element = &package->package.elements[i]; | |
168 | if (offsets[i].mode) { | |
169 | if (element->type != ACPI_TYPE_STRING && | |
170 | element->type != ACPI_TYPE_BUFFER) | |
171 | return -EFAULT; | |
172 | strncpy((u8 *)battery + offsets[i].offset, | |
173 | element->string.pointer, 32); | |
174 | } else { | |
175 | if (element->type != ACPI_TYPE_INTEGER) | |
176 | return -EFAULT; | |
177 | x = (int *)((u8 *)battery + offsets[i].offset); | |
178 | *x = element->integer.value; | |
179 | } | |
b6ce4083 | 180 | } |
b6ce4083 VL |
181 | return 0; |
182 | } | |
183 | ||
184 | static int acpi_battery_get_status(struct acpi_battery *battery) | |
185 | { | |
186 | int result = 0; | |
187 | ||
188 | result = acpi_bus_get_status(battery->device); | |
189 | if (result) { | |
190 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA")); | |
191 | return -ENODEV; | |
192 | } | |
193 | return result; | |
194 | } | |
195 | ||
196 | static int acpi_battery_get_info(struct acpi_battery *battery) | |
1da177e4 | 197 | { |
4be44fcd LB |
198 | int result = 0; |
199 | acpi_status status = 0; | |
200 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
1da177e4 | 201 | |
b6ce4083 VL |
202 | if (!acpi_battery_present(battery)) |
203 | return 0; | |
038fdea2 | 204 | mutex_lock(&battery->lock); |
f1d4661a | 205 | status = acpi_evaluate_object(battery->device->handle, "_BIF", |
038fdea2 AS |
206 | NULL, &buffer); |
207 | mutex_unlock(&battery->lock); | |
1da177e4 | 208 | if (ACPI_FAILURE(status)) { |
a6fc6720 | 209 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF")); |
d550d98d | 210 | return -ENODEV; |
1da177e4 | 211 | } |
038fdea2 AS |
212 | result = extract_package(battery, buffer.pointer, |
213 | info_offsets, ARRAY_SIZE(info_offsets)); | |
78490d82 | 214 | kfree(buffer.pointer); |
d550d98d | 215 | return result; |
1da177e4 LT |
216 | } |
217 | ||
b6ce4083 | 218 | static int acpi_battery_get_state(struct acpi_battery *battery) |
1da177e4 | 219 | { |
4be44fcd LB |
220 | int result = 0; |
221 | acpi_status status = 0; | |
222 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
1da177e4 | 223 | |
b6ce4083 VL |
224 | if (!acpi_battery_present(battery)) |
225 | return 0; | |
1da177e4 | 226 | |
f1d4661a AS |
227 | if (battery->update_time && |
228 | time_before(jiffies, battery->update_time + | |
229 | msecs_to_jiffies(cache_time))) | |
230 | return 0; | |
231 | ||
038fdea2 | 232 | mutex_lock(&battery->lock); |
f1d4661a | 233 | status = acpi_evaluate_object(battery->device->handle, "_BST", |
038fdea2 AS |
234 | NULL, &buffer); |
235 | mutex_unlock(&battery->lock); | |
5b31d895 | 236 | |
1da177e4 | 237 | if (ACPI_FAILURE(status)) { |
a6fc6720 | 238 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST")); |
d550d98d | 239 | return -ENODEV; |
1da177e4 | 240 | } |
038fdea2 AS |
241 | result = extract_package(battery, buffer.pointer, |
242 | state_offsets, ARRAY_SIZE(state_offsets)); | |
f1d4661a | 243 | battery->update_time = jiffies; |
78490d82 | 244 | kfree(buffer.pointer); |
b6ce4083 VL |
245 | return result; |
246 | } | |
1da177e4 | 247 | |
b6ce4083 VL |
248 | static int acpi_battery_get_alarm(struct acpi_battery *battery) |
249 | { | |
b6ce4083 | 250 | return 0; |
1da177e4 LT |
251 | } |
252 | ||
9ea7d575 VL |
253 | static int acpi_battery_set_alarm(struct acpi_battery *battery, |
254 | unsigned long alarm) | |
1da177e4 | 255 | { |
4be44fcd LB |
256 | acpi_status status = 0; |
257 | union acpi_object arg0 = { ACPI_TYPE_INTEGER }; | |
258 | struct acpi_object_list arg_list = { 1, &arg0 }; | |
1da177e4 | 259 | |
b6ce4083 VL |
260 | if (!acpi_battery_present(battery)) |
261 | return -ENODEV; | |
1da177e4 | 262 | |
038fdea2 | 263 | if (!battery->alarm_present) |
d550d98d | 264 | return -ENODEV; |
1da177e4 LT |
265 | |
266 | arg0.integer.value = alarm; | |
267 | ||
038fdea2 | 268 | mutex_lock(&battery->lock); |
f1d4661a AS |
269 | status = acpi_evaluate_object(battery->device->handle, "_BTP", |
270 | &arg_list, NULL); | |
038fdea2 | 271 | mutex_unlock(&battery->lock); |
1da177e4 | 272 | if (ACPI_FAILURE(status)) |
d550d98d | 273 | return -ENODEV; |
1da177e4 LT |
274 | |
275 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", (u32) alarm)); | |
276 | ||
277 | battery->alarm = alarm; | |
278 | ||
d550d98d | 279 | return 0; |
1da177e4 LT |
280 | } |
281 | ||
b6ce4083 | 282 | static int acpi_battery_init_alarm(struct acpi_battery *battery) |
1da177e4 | 283 | { |
4be44fcd LB |
284 | acpi_status status = AE_OK; |
285 | acpi_handle handle = NULL; | |
4be44fcd | 286 | |
b6ce4083 | 287 | /* See if alarms are supported, and if so, set default */ |
f1d4661a AS |
288 | status = acpi_get_handle(battery->device->handle, "_BTP", &handle); |
289 | if (ACPI_FAILURE(status)) { | |
038fdea2 | 290 | battery->alarm_present = 0; |
f1d4661a | 291 | return 0; |
b6ce4083 | 292 | } |
f1d4661a AS |
293 | battery->alarm_present = 1; |
294 | if (!battery->alarm) | |
295 | battery->alarm = battery->design_capacity_warning; | |
296 | return acpi_battery_set_alarm(battery, battery->alarm); | |
b6ce4083 | 297 | } |
1da177e4 | 298 | |
f1d4661a | 299 | static int acpi_battery_update(struct acpi_battery *battery) |
b6ce4083 | 300 | { |
f1d4661a AS |
301 | int saved_present = acpi_battery_present(battery); |
302 | int result = acpi_battery_get_status(battery); | |
303 | if (result || !acpi_battery_present(battery)) | |
b6ce4083 | 304 | return result; |
f1d4661a AS |
305 | if (saved_present != acpi_battery_present(battery) || |
306 | !battery->update_time) { | |
307 | battery->update_time = 0; | |
b6ce4083 VL |
308 | result = acpi_battery_get_info(battery); |
309 | if (result) | |
310 | return result; | |
b6ce4083 VL |
311 | acpi_battery_init_alarm(battery); |
312 | } | |
f1d4661a | 313 | return acpi_battery_get_state(battery); |
4bd35cdb VL |
314 | } |
315 | ||
1da177e4 LT |
316 | /* -------------------------------------------------------------------------- |
317 | FS Interface (/proc) | |
318 | -------------------------------------------------------------------------- */ | |
319 | ||
4be44fcd | 320 | static struct proc_dir_entry *acpi_battery_dir; |
b6ce4083 | 321 | |
78490d82 | 322 | static int acpi_battery_print_info(struct seq_file *seq, int result) |
1da177e4 | 323 | { |
50dd0969 | 324 | struct acpi_battery *battery = seq->private; |
4be44fcd | 325 | char *units = "?"; |
1da177e4 | 326 | |
b6ce4083 | 327 | if (result) |
1da177e4 LT |
328 | goto end; |
329 | ||
b6ce4083 | 330 | if (acpi_battery_present(battery)) |
1da177e4 LT |
331 | seq_printf(seq, "present: yes\n"); |
332 | else { | |
333 | seq_printf(seq, "present: no\n"); | |
334 | goto end; | |
335 | } | |
336 | ||
b6ce4083 VL |
337 | /* Battery Units */ |
338 | ||
f1d4661a | 339 | units = acpi_battery_units(battery); |
4be44fcd | 340 | |
038fdea2 | 341 | if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
1da177e4 LT |
342 | seq_printf(seq, "design capacity: unknown\n"); |
343 | else | |
344 | seq_printf(seq, "design capacity: %d %sh\n", | |
038fdea2 | 345 | (u32) battery->design_capacity, units); |
1da177e4 | 346 | |
038fdea2 | 347 | if (battery->last_full_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
1da177e4 LT |
348 | seq_printf(seq, "last full capacity: unknown\n"); |
349 | else | |
350 | seq_printf(seq, "last full capacity: %d %sh\n", | |
038fdea2 | 351 | (u32) battery->last_full_capacity, units); |
1da177e4 | 352 | |
038fdea2 | 353 | switch ((u32) battery->technology) { |
1da177e4 LT |
354 | case 0: |
355 | seq_printf(seq, "battery technology: non-rechargeable\n"); | |
356 | break; | |
357 | case 1: | |
358 | seq_printf(seq, "battery technology: rechargeable\n"); | |
359 | break; | |
360 | default: | |
361 | seq_printf(seq, "battery technology: unknown\n"); | |
362 | break; | |
363 | } | |
364 | ||
038fdea2 | 365 | if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN) |
1da177e4 LT |
366 | seq_printf(seq, "design voltage: unknown\n"); |
367 | else | |
368 | seq_printf(seq, "design voltage: %d mV\n", | |
038fdea2 | 369 | (u32) battery->design_voltage); |
1da177e4 | 370 | seq_printf(seq, "design capacity warning: %d %sh\n", |
038fdea2 | 371 | (u32) battery->design_capacity_warning, units); |
1da177e4 | 372 | seq_printf(seq, "design capacity low: %d %sh\n", |
038fdea2 | 373 | (u32) battery->design_capacity_low, units); |
1da177e4 | 374 | seq_printf(seq, "capacity granularity 1: %d %sh\n", |
038fdea2 | 375 | (u32) battery->capacity_granularity_1, units); |
1da177e4 | 376 | seq_printf(seq, "capacity granularity 2: %d %sh\n", |
038fdea2 AS |
377 | (u32) battery->capacity_granularity_2, units); |
378 | seq_printf(seq, "model number: %s\n", battery->model_number); | |
379 | seq_printf(seq, "serial number: %s\n", battery->serial_number); | |
380 | seq_printf(seq, "battery type: %s\n", battery->type); | |
381 | seq_printf(seq, "OEM info: %s\n", battery->oem_info); | |
4be44fcd LB |
382 | |
383 | end: | |
1da177e4 | 384 | |
b6ce4083 VL |
385 | if (result) |
386 | seq_printf(seq, "ERROR: Unable to read battery info\n"); | |
387 | ||
388 | return result; | |
389 | } | |
390 | ||
78490d82 | 391 | static int acpi_battery_print_state(struct seq_file *seq, int result) |
1da177e4 | 392 | { |
50dd0969 | 393 | struct acpi_battery *battery = seq->private; |
4be44fcd | 394 | char *units = "?"; |
1da177e4 | 395 | |
b6ce4083 | 396 | if (result) |
1da177e4 LT |
397 | goto end; |
398 | ||
b6ce4083 | 399 | if (acpi_battery_present(battery)) |
1da177e4 LT |
400 | seq_printf(seq, "present: yes\n"); |
401 | else { | |
402 | seq_printf(seq, "present: no\n"); | |
403 | goto end; | |
404 | } | |
405 | ||
b6ce4083 VL |
406 | /* Battery Units */ |
407 | ||
f1d4661a | 408 | units = acpi_battery_units(battery); |
b6ce4083 | 409 | |
038fdea2 | 410 | if (!(battery->state & 0x04)) |
1da177e4 LT |
411 | seq_printf(seq, "capacity state: ok\n"); |
412 | else | |
413 | seq_printf(seq, "capacity state: critical\n"); | |
414 | ||
038fdea2 | 415 | if ((battery->state & 0x01) && (battery->state & 0x02)) { |
4be44fcd LB |
416 | seq_printf(seq, |
417 | "charging state: charging/discharging\n"); | |
038fdea2 | 418 | } else if (battery->state & 0x01) |
1da177e4 | 419 | seq_printf(seq, "charging state: discharging\n"); |
038fdea2 | 420 | else if (battery->state & 0x02) |
1da177e4 LT |
421 | seq_printf(seq, "charging state: charging\n"); |
422 | else { | |
423 | seq_printf(seq, "charging state: charged\n"); | |
424 | } | |
425 | ||
038fdea2 | 426 | if (battery->present_rate == ACPI_BATTERY_VALUE_UNKNOWN) |
1da177e4 LT |
427 | seq_printf(seq, "present rate: unknown\n"); |
428 | else | |
429 | seq_printf(seq, "present rate: %d %s\n", | |
038fdea2 | 430 | (u32) battery->present_rate, units); |
1da177e4 | 431 | |
038fdea2 | 432 | if (battery->remaining_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
1da177e4 LT |
433 | seq_printf(seq, "remaining capacity: unknown\n"); |
434 | else | |
435 | seq_printf(seq, "remaining capacity: %d %sh\n", | |
038fdea2 | 436 | (u32) battery->remaining_capacity, units); |
1da177e4 | 437 | |
038fdea2 | 438 | if (battery->present_voltage == ACPI_BATTERY_VALUE_UNKNOWN) |
1da177e4 LT |
439 | seq_printf(seq, "present voltage: unknown\n"); |
440 | else | |
441 | seq_printf(seq, "present voltage: %d mV\n", | |
038fdea2 | 442 | (u32) battery->present_voltage); |
1da177e4 | 443 | |
4be44fcd | 444 | end: |
1da177e4 | 445 | |
b6ce4083 VL |
446 | if (result) { |
447 | seq_printf(seq, "ERROR: Unable to read battery state\n"); | |
448 | } | |
449 | ||
450 | return result; | |
451 | } | |
452 | ||
78490d82 | 453 | static int acpi_battery_print_alarm(struct seq_file *seq, int result) |
1da177e4 | 454 | { |
50dd0969 | 455 | struct acpi_battery *battery = seq->private; |
4be44fcd | 456 | char *units = "?"; |
1da177e4 | 457 | |
b6ce4083 | 458 | if (result) |
1da177e4 LT |
459 | goto end; |
460 | ||
b6ce4083 | 461 | if (!acpi_battery_present(battery)) { |
1da177e4 LT |
462 | seq_printf(seq, "present: no\n"); |
463 | goto end; | |
464 | } | |
465 | ||
466 | /* Battery Units */ | |
4be44fcd | 467 | |
f1d4661a | 468 | units = acpi_battery_units(battery); |
1da177e4 LT |
469 | |
470 | seq_printf(seq, "alarm: "); | |
471 | if (!battery->alarm) | |
472 | seq_printf(seq, "unsupported\n"); | |
473 | else | |
f1d4661a | 474 | seq_printf(seq, "%u %sh\n", battery->alarm, units); |
1da177e4 | 475 | |
4be44fcd | 476 | end: |
b6ce4083 VL |
477 | |
478 | if (result) | |
479 | seq_printf(seq, "ERROR: Unable to read battery alarm\n"); | |
480 | ||
481 | return result; | |
482 | } | |
483 | ||
f1d4661a AS |
484 | static ssize_t acpi_battery_write_alarm(struct file *file, |
485 | const char __user * buffer, | |
486 | size_t count, loff_t * ppos) | |
1da177e4 | 487 | { |
4be44fcd LB |
488 | int result = 0; |
489 | char alarm_string[12] = { '\0' }; | |
50dd0969 JE |
490 | struct seq_file *m = file->private_data; |
491 | struct acpi_battery *battery = m->private; | |
1da177e4 | 492 | |
1da177e4 | 493 | if (!battery || (count > sizeof(alarm_string) - 1)) |
d550d98d | 494 | return -EINVAL; |
b6ce4083 VL |
495 | if (result) { |
496 | result = -ENODEV; | |
497 | goto end; | |
498 | } | |
b6ce4083 VL |
499 | if (!acpi_battery_present(battery)) { |
500 | result = -ENODEV; | |
501 | goto end; | |
502 | } | |
b6ce4083 VL |
503 | if (copy_from_user(alarm_string, buffer, count)) { |
504 | result = -EFAULT; | |
505 | goto end; | |
506 | } | |
1da177e4 | 507 | alarm_string[count] = '\0'; |
f1d4661a AS |
508 | battery->alarm = simple_strtol(alarm_string, NULL, 0); |
509 | result = acpi_battery_set_alarm(battery, battery->alarm); | |
b6ce4083 | 510 | end: |
b6ce4083 | 511 | if (!result) |
f1d4661a | 512 | return count; |
78490d82 AS |
513 | return result; |
514 | } | |
515 | ||
516 | typedef int(*print_func)(struct seq_file *seq, int result); | |
517 | typedef int(*get_func)(struct acpi_battery *battery); | |
518 | ||
519 | static struct acpi_read_mux { | |
520 | print_func print; | |
521 | get_func get; | |
522 | } acpi_read_funcs[ACPI_BATTERY_NUMFILES] = { | |
523 | {.get = acpi_battery_get_info, .print = acpi_battery_print_info}, | |
524 | {.get = acpi_battery_get_state, .print = acpi_battery_print_state}, | |
525 | {.get = acpi_battery_get_alarm, .print = acpi_battery_print_alarm}, | |
526 | }; | |
b6ce4083 | 527 | |
78490d82 AS |
528 | static int acpi_battery_read(int fid, struct seq_file *seq) |
529 | { | |
530 | struct acpi_battery *battery = seq->private; | |
f1d4661a AS |
531 | int result = acpi_battery_update(battery); |
532 | return acpi_read_funcs[fid].print(seq, result); | |
1da177e4 LT |
533 | } |
534 | ||
78490d82 AS |
535 | static int acpi_battery_read_info(struct seq_file *seq, void *offset) |
536 | { | |
537 | return acpi_battery_read(ACPI_BATTERY_INFO, seq); | |
538 | } | |
539 | ||
540 | static int acpi_battery_read_state(struct seq_file *seq, void *offset) | |
541 | { | |
542 | return acpi_battery_read(ACPI_BATTERY_STATE, seq); | |
543 | } | |
544 | ||
545 | static int acpi_battery_read_alarm(struct seq_file *seq, void *offset) | |
546 | { | |
547 | return acpi_battery_read(ACPI_BATTERY_ALARM, seq); | |
548 | } | |
549 | ||
550 | static int acpi_battery_info_open_fs(struct inode *inode, struct file *file) | |
551 | { | |
552 | return single_open(file, acpi_battery_read_info, PDE(inode)->data); | |
553 | } | |
554 | ||
555 | static int acpi_battery_state_open_fs(struct inode *inode, struct file *file) | |
556 | { | |
557 | return single_open(file, acpi_battery_read_state, PDE(inode)->data); | |
558 | } | |
559 | ||
1da177e4 LT |
560 | static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file) |
561 | { | |
562 | return single_open(file, acpi_battery_read_alarm, PDE(inode)->data); | |
563 | } | |
564 | ||
78490d82 AS |
565 | static struct battery_file { |
566 | struct file_operations ops; | |
567 | mode_t mode; | |
568 | char *name; | |
569 | } acpi_battery_file[] = { | |
570 | { | |
571 | .name = "info", | |
572 | .mode = S_IRUGO, | |
573 | .ops = { | |
4be44fcd LB |
574 | .open = acpi_battery_info_open_fs, |
575 | .read = seq_read, | |
576 | .llseek = seq_lseek, | |
577 | .release = single_release, | |
1da177e4 | 578 | .owner = THIS_MODULE, |
78490d82 AS |
579 | }, |
580 | }, | |
581 | { | |
582 | .name = "state", | |
583 | .mode = S_IRUGO, | |
584 | .ops = { | |
4be44fcd LB |
585 | .open = acpi_battery_state_open_fs, |
586 | .read = seq_read, | |
587 | .llseek = seq_lseek, | |
588 | .release = single_release, | |
1da177e4 | 589 | .owner = THIS_MODULE, |
78490d82 AS |
590 | }, |
591 | }, | |
592 | { | |
593 | .name = "alarm", | |
594 | .mode = S_IFREG | S_IRUGO | S_IWUSR, | |
595 | .ops = { | |
4be44fcd LB |
596 | .open = acpi_battery_alarm_open_fs, |
597 | .read = seq_read, | |
598 | .write = acpi_battery_write_alarm, | |
599 | .llseek = seq_lseek, | |
600 | .release = single_release, | |
1da177e4 | 601 | .owner = THIS_MODULE, |
78490d82 AS |
602 | }, |
603 | }, | |
1da177e4 LT |
604 | }; |
605 | ||
4be44fcd | 606 | static int acpi_battery_add_fs(struct acpi_device *device) |
1da177e4 | 607 | { |
4be44fcd | 608 | struct proc_dir_entry *entry = NULL; |
78490d82 | 609 | int i; |
1da177e4 | 610 | |
1da177e4 LT |
611 | if (!acpi_device_dir(device)) { |
612 | acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), | |
4be44fcd | 613 | acpi_battery_dir); |
1da177e4 | 614 | if (!acpi_device_dir(device)) |
d550d98d | 615 | return -ENODEV; |
1da177e4 LT |
616 | acpi_device_dir(device)->owner = THIS_MODULE; |
617 | } | |
618 | ||
78490d82 AS |
619 | for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) { |
620 | entry = create_proc_entry(acpi_battery_file[i].name, | |
621 | acpi_battery_file[i].mode, acpi_device_dir(device)); | |
622 | if (!entry) | |
623 | return -ENODEV; | |
624 | else { | |
625 | entry->proc_fops = &acpi_battery_file[i].ops; | |
626 | entry->data = acpi_driver_data(device); | |
627 | entry->owner = THIS_MODULE; | |
628 | } | |
1da177e4 LT |
629 | } |
630 | ||
d550d98d | 631 | return 0; |
1da177e4 LT |
632 | } |
633 | ||
4be44fcd | 634 | static int acpi_battery_remove_fs(struct acpi_device *device) |
1da177e4 | 635 | { |
78490d82 | 636 | int i; |
1da177e4 | 637 | if (acpi_device_dir(device)) { |
78490d82 AS |
638 | for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) { |
639 | remove_proc_entry(acpi_battery_file[i].name, | |
1da177e4 | 640 | acpi_device_dir(device)); |
78490d82 | 641 | } |
1da177e4 LT |
642 | remove_proc_entry(acpi_device_bid(device), acpi_battery_dir); |
643 | acpi_device_dir(device) = NULL; | |
644 | } | |
645 | ||
d550d98d | 646 | return 0; |
1da177e4 LT |
647 | } |
648 | ||
1da177e4 LT |
649 | /* -------------------------------------------------------------------------- |
650 | Driver Interface | |
651 | -------------------------------------------------------------------------- */ | |
652 | ||
4be44fcd | 653 | static void acpi_battery_notify(acpi_handle handle, u32 event, void *data) |
1da177e4 | 654 | { |
50dd0969 | 655 | struct acpi_battery *battery = data; |
f1d4661a | 656 | struct acpi_device *device; |
1da177e4 | 657 | if (!battery) |
d550d98d | 658 | return; |
145def84 | 659 | device = battery->device; |
f1d4661a AS |
660 | acpi_battery_update(battery); |
661 | acpi_bus_generate_proc_event(device, event, | |
662 | acpi_battery_present(battery)); | |
663 | acpi_bus_generate_netlink_event(device->pnp.device_class, | |
664 | device->dev.bus_id, event, | |
9ea7d575 | 665 | acpi_battery_present(battery)); |
1da177e4 LT |
666 | } |
667 | ||
4be44fcd | 668 | static int acpi_battery_add(struct acpi_device *device) |
1da177e4 | 669 | { |
4be44fcd LB |
670 | int result = 0; |
671 | acpi_status status = 0; | |
672 | struct acpi_battery *battery = NULL; | |
1da177e4 | 673 | |
1da177e4 | 674 | if (!device) |
d550d98d | 675 | return -EINVAL; |
1da177e4 | 676 | |
36bcbec7 | 677 | battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL); |
1da177e4 | 678 | if (!battery) |
d550d98d | 679 | return -ENOMEM; |
1da177e4 | 680 | |
145def84 | 681 | battery->device = device; |
1da177e4 LT |
682 | strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME); |
683 | strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS); | |
684 | acpi_driver_data(device) = battery; | |
038fdea2 | 685 | mutex_init(&battery->lock); |
f1d4661a | 686 | acpi_battery_update(battery); |
1da177e4 LT |
687 | result = acpi_battery_add_fs(device); |
688 | if (result) | |
689 | goto end; | |
690 | ||
3b073ec3 | 691 | status = acpi_install_notify_handler(device->handle, |
9fdae727 | 692 | ACPI_ALL_NOTIFY, |
4be44fcd | 693 | acpi_battery_notify, battery); |
1da177e4 | 694 | if (ACPI_FAILURE(status)) { |
b6ce4083 | 695 | ACPI_EXCEPTION((AE_INFO, status, "Installing notify handler")); |
1da177e4 LT |
696 | result = -ENODEV; |
697 | goto end; | |
698 | } | |
699 | ||
700 | printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n", | |
4be44fcd LB |
701 | ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device), |
702 | device->status.battery_present ? "present" : "absent"); | |
703 | ||
704 | end: | |
b6ce4083 | 705 | |
1da177e4 LT |
706 | if (result) { |
707 | acpi_battery_remove_fs(device); | |
708 | kfree(battery); | |
709 | } | |
710 | ||
d550d98d | 711 | return result; |
1da177e4 LT |
712 | } |
713 | ||
4be44fcd | 714 | static int acpi_battery_remove(struct acpi_device *device, int type) |
1da177e4 | 715 | { |
4be44fcd LB |
716 | acpi_status status = 0; |
717 | struct acpi_battery *battery = NULL; | |
1da177e4 | 718 | |
1da177e4 | 719 | if (!device || !acpi_driver_data(device)) |
d550d98d | 720 | return -EINVAL; |
1da177e4 | 721 | |
50dd0969 | 722 | battery = acpi_driver_data(device); |
1da177e4 | 723 | |
3b073ec3 | 724 | status = acpi_remove_notify_handler(device->handle, |
9fdae727 | 725 | ACPI_ALL_NOTIFY, |
4be44fcd | 726 | acpi_battery_notify); |
1da177e4 LT |
727 | |
728 | acpi_battery_remove_fs(device); | |
038fdea2 | 729 | mutex_destroy(&battery->lock); |
1da177e4 LT |
730 | kfree(battery); |
731 | ||
d550d98d | 732 | return 0; |
1da177e4 LT |
733 | } |
734 | ||
34c4415a | 735 | /* this is needed to learn about changes made in suspended state */ |
5d9464a4 | 736 | static int acpi_battery_resume(struct acpi_device *device) |
34c4415a JK |
737 | { |
738 | struct acpi_battery *battery; | |
34c4415a JK |
739 | if (!device) |
740 | return -EINVAL; | |
f1d4661a AS |
741 | battery = acpi_driver_data(device); |
742 | battery->update_time = 0; | |
b6ce4083 | 743 | return 0; |
34c4415a JK |
744 | } |
745 | ||
4be44fcd | 746 | static int __init acpi_battery_init(void) |
1da177e4 | 747 | { |
3f86b832 | 748 | int result; |
1da177e4 | 749 | |
4d8316d5 PM |
750 | if (acpi_disabled) |
751 | return -ENODEV; | |
752 | ||
3f86b832 | 753 | acpi_battery_dir = acpi_lock_battery_dir(); |
1da177e4 | 754 | if (!acpi_battery_dir) |
d550d98d | 755 | return -ENODEV; |
1da177e4 LT |
756 | |
757 | result = acpi_bus_register_driver(&acpi_battery_driver); | |
758 | if (result < 0) { | |
3f86b832 | 759 | acpi_unlock_battery_dir(acpi_battery_dir); |
d550d98d | 760 | return -ENODEV; |
1da177e4 LT |
761 | } |
762 | ||
d550d98d | 763 | return 0; |
1da177e4 LT |
764 | } |
765 | ||
4be44fcd | 766 | static void __exit acpi_battery_exit(void) |
1da177e4 | 767 | { |
1da177e4 LT |
768 | acpi_bus_unregister_driver(&acpi_battery_driver); |
769 | ||
3f86b832 | 770 | acpi_unlock_battery_dir(acpi_battery_dir); |
1da177e4 | 771 | |
d550d98d | 772 | return; |
1da177e4 LT |
773 | } |
774 | ||
1da177e4 LT |
775 | module_init(acpi_battery_init); |
776 | module_exit(acpi_battery_exit); |