]>
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> | |
30 | #include <linux/proc_fs.h> | |
31 | #include <linux/seq_file.h> | |
32 | #include <asm/uaccess.h> | |
33 | ||
34 | #include <acpi/acpi_bus.h> | |
35 | #include <acpi/acpi_drivers.h> | |
36 | ||
1da177e4 LT |
37 | #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF |
38 | ||
39 | #define ACPI_BATTERY_FORMAT_BIF "NNNNNNNNNSSSS" | |
40 | #define ACPI_BATTERY_FORMAT_BST "NNNN" | |
41 | ||
42 | #define ACPI_BATTERY_COMPONENT 0x00040000 | |
43 | #define ACPI_BATTERY_CLASS "battery" | |
44 | #define ACPI_BATTERY_HID "PNP0C0A" | |
45 | #define ACPI_BATTERY_DRIVER_NAME "ACPI Battery Driver" | |
46 | #define ACPI_BATTERY_DEVICE_NAME "Battery" | |
47 | #define ACPI_BATTERY_FILE_INFO "info" | |
48 | #define ACPI_BATTERY_FILE_STATUS "state" | |
49 | #define ACPI_BATTERY_FILE_ALARM "alarm" | |
50 | #define ACPI_BATTERY_NOTIFY_STATUS 0x80 | |
51 | #define ACPI_BATTERY_NOTIFY_INFO 0x81 | |
52 | #define ACPI_BATTERY_UNITS_WATTS "mW" | |
53 | #define ACPI_BATTERY_UNITS_AMPS "mA" | |
54 | ||
1da177e4 | 55 | #define _COMPONENT ACPI_BATTERY_COMPONENT |
4be44fcd | 56 | ACPI_MODULE_NAME("acpi_battery") |
1da177e4 | 57 | |
4be44fcd | 58 | MODULE_AUTHOR("Paul Diefenbaugh"); |
1da177e4 LT |
59 | MODULE_DESCRIPTION(ACPI_BATTERY_DRIVER_NAME); |
60 | MODULE_LICENSE("GPL"); | |
61 | ||
4be44fcd LB |
62 | static int acpi_battery_add(struct acpi_device *device); |
63 | static int acpi_battery_remove(struct acpi_device *device, int type); | |
1da177e4 LT |
64 | |
65 | static struct acpi_driver acpi_battery_driver = { | |
4be44fcd LB |
66 | .name = ACPI_BATTERY_DRIVER_NAME, |
67 | .class = ACPI_BATTERY_CLASS, | |
68 | .ids = ACPI_BATTERY_HID, | |
69 | .ops = { | |
70 | .add = acpi_battery_add, | |
71 | .remove = acpi_battery_remove, | |
72 | }, | |
1da177e4 LT |
73 | }; |
74 | ||
75 | struct acpi_battery_status { | |
4be44fcd LB |
76 | acpi_integer state; |
77 | acpi_integer present_rate; | |
78 | acpi_integer remaining_capacity; | |
79 | acpi_integer present_voltage; | |
1da177e4 LT |
80 | }; |
81 | ||
82 | struct acpi_battery_info { | |
4be44fcd LB |
83 | acpi_integer power_unit; |
84 | acpi_integer design_capacity; | |
85 | acpi_integer last_full_capacity; | |
86 | acpi_integer battery_technology; | |
87 | acpi_integer design_voltage; | |
88 | acpi_integer design_capacity_warning; | |
89 | acpi_integer design_capacity_low; | |
90 | acpi_integer battery_capacity_granularity_1; | |
91 | acpi_integer battery_capacity_granularity_2; | |
92 | acpi_string model_number; | |
93 | acpi_string serial_number; | |
94 | acpi_string battery_type; | |
95 | acpi_string oem_info; | |
1da177e4 LT |
96 | }; |
97 | ||
98 | struct acpi_battery_flags { | |
4be44fcd LB |
99 | u8 present:1; /* Bay occupied? */ |
100 | u8 power_unit:1; /* 0=watts, 1=apms */ | |
101 | u8 alarm:1; /* _BTP present? */ | |
102 | u8 reserved:5; | |
1da177e4 LT |
103 | }; |
104 | ||
105 | struct acpi_battery_trips { | |
4be44fcd LB |
106 | unsigned long warning; |
107 | unsigned long low; | |
1da177e4 LT |
108 | }; |
109 | ||
110 | struct acpi_battery { | |
4be44fcd | 111 | acpi_handle handle; |
1da177e4 LT |
112 | struct acpi_battery_flags flags; |
113 | struct acpi_battery_trips trips; | |
4be44fcd | 114 | unsigned long alarm; |
1da177e4 LT |
115 | struct acpi_battery_info *info; |
116 | }; | |
117 | ||
1da177e4 LT |
118 | /* -------------------------------------------------------------------------- |
119 | Battery Management | |
120 | -------------------------------------------------------------------------- */ | |
121 | ||
122 | static int | |
4be44fcd LB |
123 | acpi_battery_get_info(struct acpi_battery *battery, |
124 | struct acpi_battery_info **bif) | |
1da177e4 | 125 | { |
4be44fcd LB |
126 | int result = 0; |
127 | acpi_status status = 0; | |
128 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
129 | struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BIF), | |
130 | ACPI_BATTERY_FORMAT_BIF | |
131 | }; | |
132 | struct acpi_buffer data = { 0, NULL }; | |
133 | union acpi_object *package = NULL; | |
1da177e4 | 134 | |
1da177e4 LT |
135 | |
136 | if (!battery || !bif) | |
d550d98d | 137 | return -EINVAL; |
1da177e4 LT |
138 | |
139 | /* Evalute _BIF */ | |
140 | ||
141 | status = acpi_evaluate_object(battery->handle, "_BIF", NULL, &buffer); | |
142 | if (ACPI_FAILURE(status)) { | |
a6fc6720 | 143 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF")); |
d550d98d | 144 | return -ENODEV; |
1da177e4 LT |
145 | } |
146 | ||
4be44fcd | 147 | package = (union acpi_object *)buffer.pointer; |
1da177e4 LT |
148 | |
149 | /* Extract Package Data */ | |
150 | ||
151 | status = acpi_extract_package(package, &format, &data); | |
152 | if (status != AE_BUFFER_OVERFLOW) { | |
a6fc6720 | 153 | ACPI_EXCEPTION((AE_INFO, status, "Extracting _BIF")); |
1da177e4 LT |
154 | result = -ENODEV; |
155 | goto end; | |
156 | } | |
157 | ||
158 | data.pointer = kmalloc(data.length, GFP_KERNEL); | |
159 | if (!data.pointer) { | |
160 | result = -ENOMEM; | |
161 | goto end; | |
162 | } | |
163 | memset(data.pointer, 0, data.length); | |
164 | ||
165 | status = acpi_extract_package(package, &format, &data); | |
166 | if (ACPI_FAILURE(status)) { | |
a6fc6720 | 167 | ACPI_EXCEPTION((AE_INFO, status, "Extracting _BIF")); |
1da177e4 LT |
168 | kfree(data.pointer); |
169 | result = -ENODEV; | |
170 | goto end; | |
171 | } | |
172 | ||
4be44fcd | 173 | end: |
1da177e4 LT |
174 | acpi_os_free(buffer.pointer); |
175 | ||
176 | if (!result) | |
4be44fcd | 177 | (*bif) = (struct acpi_battery_info *)data.pointer; |
1da177e4 | 178 | |
d550d98d | 179 | return result; |
1da177e4 LT |
180 | } |
181 | ||
182 | static int | |
4be44fcd LB |
183 | acpi_battery_get_status(struct acpi_battery *battery, |
184 | struct acpi_battery_status **bst) | |
1da177e4 | 185 | { |
4be44fcd LB |
186 | int result = 0; |
187 | acpi_status status = 0; | |
188 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
189 | struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BST), | |
190 | ACPI_BATTERY_FORMAT_BST | |
191 | }; | |
192 | struct acpi_buffer data = { 0, NULL }; | |
193 | union acpi_object *package = NULL; | |
1da177e4 | 194 | |
1da177e4 LT |
195 | |
196 | if (!battery || !bst) | |
d550d98d | 197 | return -EINVAL; |
1da177e4 LT |
198 | |
199 | /* Evalute _BST */ | |
200 | ||
201 | status = acpi_evaluate_object(battery->handle, "_BST", NULL, &buffer); | |
202 | if (ACPI_FAILURE(status)) { | |
a6fc6720 | 203 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST")); |
d550d98d | 204 | return -ENODEV; |
1da177e4 LT |
205 | } |
206 | ||
4be44fcd | 207 | package = (union acpi_object *)buffer.pointer; |
1da177e4 LT |
208 | |
209 | /* Extract Package Data */ | |
210 | ||
211 | status = acpi_extract_package(package, &format, &data); | |
212 | if (status != AE_BUFFER_OVERFLOW) { | |
a6fc6720 | 213 | ACPI_EXCEPTION((AE_INFO, status, "Extracting _BST")); |
1da177e4 LT |
214 | result = -ENODEV; |
215 | goto end; | |
216 | } | |
217 | ||
218 | data.pointer = kmalloc(data.length, GFP_KERNEL); | |
219 | if (!data.pointer) { | |
220 | result = -ENOMEM; | |
221 | goto end; | |
222 | } | |
223 | memset(data.pointer, 0, data.length); | |
224 | ||
225 | status = acpi_extract_package(package, &format, &data); | |
226 | if (ACPI_FAILURE(status)) { | |
a6fc6720 | 227 | ACPI_EXCEPTION((AE_INFO, status, "Extracting _BST")); |
1da177e4 LT |
228 | kfree(data.pointer); |
229 | result = -ENODEV; | |
230 | goto end; | |
231 | } | |
232 | ||
4be44fcd | 233 | end: |
1da177e4 LT |
234 | acpi_os_free(buffer.pointer); |
235 | ||
236 | if (!result) | |
4be44fcd | 237 | (*bst) = (struct acpi_battery_status *)data.pointer; |
1da177e4 | 238 | |
d550d98d | 239 | return result; |
1da177e4 LT |
240 | } |
241 | ||
1da177e4 | 242 | static int |
4be44fcd | 243 | acpi_battery_set_alarm(struct acpi_battery *battery, unsigned long alarm) |
1da177e4 | 244 | { |
4be44fcd LB |
245 | acpi_status status = 0; |
246 | union acpi_object arg0 = { ACPI_TYPE_INTEGER }; | |
247 | struct acpi_object_list arg_list = { 1, &arg0 }; | |
1da177e4 | 248 | |
1da177e4 LT |
249 | |
250 | if (!battery) | |
d550d98d | 251 | return -EINVAL; |
1da177e4 LT |
252 | |
253 | if (!battery->flags.alarm) | |
d550d98d | 254 | return -ENODEV; |
1da177e4 LT |
255 | |
256 | arg0.integer.value = alarm; | |
257 | ||
258 | status = acpi_evaluate_object(battery->handle, "_BTP", &arg_list, NULL); | |
259 | if (ACPI_FAILURE(status)) | |
d550d98d | 260 | return -ENODEV; |
1da177e4 LT |
261 | |
262 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", (u32) alarm)); | |
263 | ||
264 | battery->alarm = alarm; | |
265 | ||
d550d98d | 266 | return 0; |
1da177e4 LT |
267 | } |
268 | ||
4be44fcd | 269 | static int acpi_battery_check(struct acpi_battery *battery) |
1da177e4 | 270 | { |
4be44fcd LB |
271 | int result = 0; |
272 | acpi_status status = AE_OK; | |
273 | acpi_handle handle = NULL; | |
274 | struct acpi_device *device = NULL; | |
1da177e4 LT |
275 | struct acpi_battery_info *bif = NULL; |
276 | ||
4be44fcd | 277 | |
1da177e4 | 278 | if (!battery) |
d550d98d | 279 | return -EINVAL; |
1da177e4 LT |
280 | |
281 | result = acpi_bus_get_device(battery->handle, &device); | |
282 | if (result) | |
d550d98d | 283 | return result; |
1da177e4 LT |
284 | |
285 | result = acpi_bus_get_status(device); | |
286 | if (result) | |
d550d98d | 287 | return result; |
1da177e4 LT |
288 | |
289 | /* Insertion? */ | |
290 | ||
291 | if (!battery->flags.present && device->status.battery_present) { | |
292 | ||
293 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Battery inserted\n")); | |
294 | ||
295 | /* Evalute _BIF to get certain static information */ | |
296 | ||
297 | result = acpi_battery_get_info(battery, &bif); | |
298 | if (result) | |
d550d98d | 299 | return result; |
1da177e4 LT |
300 | |
301 | battery->flags.power_unit = bif->power_unit; | |
302 | battery->trips.warning = bif->design_capacity_warning; | |
303 | battery->trips.low = bif->design_capacity_low; | |
304 | kfree(bif); | |
305 | ||
306 | /* See if alarms are supported, and if so, set default */ | |
307 | ||
308 | status = acpi_get_handle(battery->handle, "_BTP", &handle); | |
309 | if (ACPI_SUCCESS(status)) { | |
310 | battery->flags.alarm = 1; | |
311 | acpi_battery_set_alarm(battery, battery->trips.warning); | |
312 | } | |
313 | } | |
314 | ||
315 | /* Removal? */ | |
316 | ||
317 | else if (battery->flags.present && !device->status.battery_present) { | |
318 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Battery removed\n")); | |
319 | } | |
320 | ||
321 | battery->flags.present = device->status.battery_present; | |
322 | ||
d550d98d | 323 | return result; |
1da177e4 LT |
324 | } |
325 | ||
1da177e4 LT |
326 | /* -------------------------------------------------------------------------- |
327 | FS Interface (/proc) | |
328 | -------------------------------------------------------------------------- */ | |
329 | ||
4be44fcd | 330 | static struct proc_dir_entry *acpi_battery_dir; |
1da177e4 LT |
331 | static int acpi_battery_read_info(struct seq_file *seq, void *offset) |
332 | { | |
4be44fcd LB |
333 | int result = 0; |
334 | struct acpi_battery *battery = (struct acpi_battery *)seq->private; | |
1da177e4 | 335 | struct acpi_battery_info *bif = NULL; |
4be44fcd | 336 | char *units = "?"; |
1da177e4 | 337 | |
1da177e4 LT |
338 | |
339 | if (!battery) | |
340 | goto end; | |
341 | ||
342 | if (battery->flags.present) | |
343 | seq_printf(seq, "present: yes\n"); | |
344 | else { | |
345 | seq_printf(seq, "present: no\n"); | |
346 | goto end; | |
347 | } | |
348 | ||
349 | /* Battery Info (_BIF) */ | |
350 | ||
351 | result = acpi_battery_get_info(battery, &bif); | |
352 | if (result || !bif) { | |
353 | seq_printf(seq, "ERROR: Unable to read battery information\n"); | |
354 | goto end; | |
355 | } | |
356 | ||
4be44fcd LB |
357 | units = |
358 | bif-> | |
359 | power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS; | |
360 | ||
1da177e4 LT |
361 | if (bif->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
362 | seq_printf(seq, "design capacity: unknown\n"); | |
363 | else | |
364 | seq_printf(seq, "design capacity: %d %sh\n", | |
4be44fcd | 365 | (u32) bif->design_capacity, units); |
1da177e4 LT |
366 | |
367 | if (bif->last_full_capacity == ACPI_BATTERY_VALUE_UNKNOWN) | |
368 | seq_printf(seq, "last full capacity: unknown\n"); | |
369 | else | |
370 | seq_printf(seq, "last full capacity: %d %sh\n", | |
4be44fcd | 371 | (u32) bif->last_full_capacity, units); |
1da177e4 LT |
372 | |
373 | switch ((u32) bif->battery_technology) { | |
374 | case 0: | |
375 | seq_printf(seq, "battery technology: non-rechargeable\n"); | |
376 | break; | |
377 | case 1: | |
378 | seq_printf(seq, "battery technology: rechargeable\n"); | |
379 | break; | |
380 | default: | |
381 | seq_printf(seq, "battery technology: unknown\n"); | |
382 | break; | |
383 | } | |
384 | ||
385 | if (bif->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN) | |
386 | seq_printf(seq, "design voltage: unknown\n"); | |
387 | else | |
388 | seq_printf(seq, "design voltage: %d mV\n", | |
4be44fcd LB |
389 | (u32) bif->design_voltage); |
390 | ||
1da177e4 | 391 | seq_printf(seq, "design capacity warning: %d %sh\n", |
4be44fcd | 392 | (u32) bif->design_capacity_warning, units); |
1da177e4 | 393 | seq_printf(seq, "design capacity low: %d %sh\n", |
4be44fcd | 394 | (u32) bif->design_capacity_low, units); |
1da177e4 | 395 | seq_printf(seq, "capacity granularity 1: %d %sh\n", |
4be44fcd | 396 | (u32) bif->battery_capacity_granularity_1, units); |
1da177e4 | 397 | seq_printf(seq, "capacity granularity 2: %d %sh\n", |
4be44fcd LB |
398 | (u32) bif->battery_capacity_granularity_2, units); |
399 | seq_printf(seq, "model number: %s\n", bif->model_number); | |
400 | seq_printf(seq, "serial number: %s\n", bif->serial_number); | |
401 | seq_printf(seq, "battery type: %s\n", bif->battery_type); | |
402 | seq_printf(seq, "OEM info: %s\n", bif->oem_info); | |
403 | ||
404 | end: | |
1da177e4 LT |
405 | kfree(bif); |
406 | ||
d550d98d | 407 | return 0; |
1da177e4 LT |
408 | } |
409 | ||
410 | static int acpi_battery_info_open_fs(struct inode *inode, struct file *file) | |
411 | { | |
412 | return single_open(file, acpi_battery_read_info, PDE(inode)->data); | |
413 | } | |
414 | ||
4be44fcd | 415 | static int acpi_battery_read_state(struct seq_file *seq, void *offset) |
1da177e4 | 416 | { |
4be44fcd LB |
417 | int result = 0; |
418 | struct acpi_battery *battery = (struct acpi_battery *)seq->private; | |
1da177e4 | 419 | struct acpi_battery_status *bst = NULL; |
4be44fcd | 420 | char *units = "?"; |
1da177e4 | 421 | |
1da177e4 LT |
422 | |
423 | if (!battery) | |
424 | goto end; | |
425 | ||
426 | if (battery->flags.present) | |
427 | seq_printf(seq, "present: yes\n"); | |
428 | else { | |
429 | seq_printf(seq, "present: no\n"); | |
430 | goto end; | |
431 | } | |
432 | ||
433 | /* Battery Units */ | |
434 | ||
4be44fcd LB |
435 | units = |
436 | battery->flags. | |
437 | power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS; | |
1da177e4 LT |
438 | |
439 | /* Battery Status (_BST) */ | |
440 | ||
441 | result = acpi_battery_get_status(battery, &bst); | |
442 | if (result || !bst) { | |
443 | seq_printf(seq, "ERROR: Unable to read battery status\n"); | |
444 | goto end; | |
445 | } | |
446 | ||
447 | if (!(bst->state & 0x04)) | |
448 | seq_printf(seq, "capacity state: ok\n"); | |
449 | else | |
450 | seq_printf(seq, "capacity state: critical\n"); | |
451 | ||
4be44fcd LB |
452 | if ((bst->state & 0x01) && (bst->state & 0x02)) { |
453 | seq_printf(seq, | |
454 | "charging state: charging/discharging\n"); | |
4be44fcd | 455 | } else if (bst->state & 0x01) |
1da177e4 LT |
456 | seq_printf(seq, "charging state: discharging\n"); |
457 | else if (bst->state & 0x02) | |
458 | seq_printf(seq, "charging state: charging\n"); | |
459 | else { | |
460 | seq_printf(seq, "charging state: charged\n"); | |
461 | } | |
462 | ||
463 | if (bst->present_rate == ACPI_BATTERY_VALUE_UNKNOWN) | |
464 | seq_printf(seq, "present rate: unknown\n"); | |
465 | else | |
466 | seq_printf(seq, "present rate: %d %s\n", | |
4be44fcd | 467 | (u32) bst->present_rate, units); |
1da177e4 LT |
468 | |
469 | if (bst->remaining_capacity == ACPI_BATTERY_VALUE_UNKNOWN) | |
470 | seq_printf(seq, "remaining capacity: unknown\n"); | |
471 | else | |
472 | seq_printf(seq, "remaining capacity: %d %sh\n", | |
4be44fcd | 473 | (u32) bst->remaining_capacity, units); |
1da177e4 LT |
474 | |
475 | if (bst->present_voltage == ACPI_BATTERY_VALUE_UNKNOWN) | |
476 | seq_printf(seq, "present voltage: unknown\n"); | |
477 | else | |
478 | seq_printf(seq, "present voltage: %d mV\n", | |
4be44fcd | 479 | (u32) bst->present_voltage); |
1da177e4 | 480 | |
4be44fcd | 481 | end: |
1da177e4 LT |
482 | kfree(bst); |
483 | ||
d550d98d | 484 | return 0; |
1da177e4 LT |
485 | } |
486 | ||
487 | static int acpi_battery_state_open_fs(struct inode *inode, struct file *file) | |
488 | { | |
489 | return single_open(file, acpi_battery_read_state, PDE(inode)->data); | |
490 | } | |
491 | ||
4be44fcd | 492 | static int acpi_battery_read_alarm(struct seq_file *seq, void *offset) |
1da177e4 | 493 | { |
4be44fcd LB |
494 | struct acpi_battery *battery = (struct acpi_battery *)seq->private; |
495 | char *units = "?"; | |
1da177e4 | 496 | |
1da177e4 LT |
497 | |
498 | if (!battery) | |
499 | goto end; | |
500 | ||
501 | if (!battery->flags.present) { | |
502 | seq_printf(seq, "present: no\n"); | |
503 | goto end; | |
504 | } | |
505 | ||
506 | /* Battery Units */ | |
4be44fcd LB |
507 | |
508 | units = | |
509 | battery->flags. | |
510 | power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS; | |
1da177e4 LT |
511 | |
512 | /* Battery Alarm */ | |
513 | ||
514 | seq_printf(seq, "alarm: "); | |
515 | if (!battery->alarm) | |
516 | seq_printf(seq, "unsupported\n"); | |
517 | else | |
518 | seq_printf(seq, "%d %sh\n", (u32) battery->alarm, units); | |
519 | ||
4be44fcd | 520 | end: |
d550d98d | 521 | return 0; |
1da177e4 LT |
522 | } |
523 | ||
1da177e4 | 524 | static ssize_t |
4be44fcd LB |
525 | acpi_battery_write_alarm(struct file *file, |
526 | const char __user * buffer, | |
527 | size_t count, loff_t * ppos) | |
1da177e4 | 528 | { |
4be44fcd LB |
529 | int result = 0; |
530 | char alarm_string[12] = { '\0' }; | |
531 | struct seq_file *m = (struct seq_file *)file->private_data; | |
532 | struct acpi_battery *battery = (struct acpi_battery *)m->private; | |
1da177e4 | 533 | |
1da177e4 LT |
534 | |
535 | if (!battery || (count > sizeof(alarm_string) - 1)) | |
d550d98d | 536 | return -EINVAL; |
1da177e4 LT |
537 | |
538 | if (!battery->flags.present) | |
d550d98d | 539 | return -ENODEV; |
1da177e4 LT |
540 | |
541 | if (copy_from_user(alarm_string, buffer, count)) | |
d550d98d | 542 | return -EFAULT; |
4be44fcd | 543 | |
1da177e4 LT |
544 | alarm_string[count] = '\0'; |
545 | ||
4be44fcd LB |
546 | result = acpi_battery_set_alarm(battery, |
547 | simple_strtoul(alarm_string, NULL, 0)); | |
1da177e4 | 548 | if (result) |
d550d98d | 549 | return result; |
1da177e4 | 550 | |
d550d98d | 551 | return count; |
1da177e4 LT |
552 | } |
553 | ||
554 | static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file) | |
555 | { | |
556 | return single_open(file, acpi_battery_read_alarm, PDE(inode)->data); | |
557 | } | |
558 | ||
559 | static struct file_operations acpi_battery_info_ops = { | |
4be44fcd LB |
560 | .open = acpi_battery_info_open_fs, |
561 | .read = seq_read, | |
562 | .llseek = seq_lseek, | |
563 | .release = single_release, | |
1da177e4 LT |
564 | .owner = THIS_MODULE, |
565 | }; | |
566 | ||
567 | static struct file_operations acpi_battery_state_ops = { | |
4be44fcd LB |
568 | .open = acpi_battery_state_open_fs, |
569 | .read = seq_read, | |
570 | .llseek = seq_lseek, | |
571 | .release = single_release, | |
1da177e4 LT |
572 | .owner = THIS_MODULE, |
573 | }; | |
574 | ||
575 | static struct file_operations acpi_battery_alarm_ops = { | |
4be44fcd LB |
576 | .open = acpi_battery_alarm_open_fs, |
577 | .read = seq_read, | |
578 | .write = acpi_battery_write_alarm, | |
579 | .llseek = seq_lseek, | |
580 | .release = single_release, | |
1da177e4 LT |
581 | .owner = THIS_MODULE, |
582 | }; | |
583 | ||
4be44fcd | 584 | static int acpi_battery_add_fs(struct acpi_device *device) |
1da177e4 | 585 | { |
4be44fcd | 586 | struct proc_dir_entry *entry = NULL; |
1da177e4 | 587 | |
1da177e4 LT |
588 | |
589 | if (!acpi_device_dir(device)) { | |
590 | acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), | |
4be44fcd | 591 | acpi_battery_dir); |
1da177e4 | 592 | if (!acpi_device_dir(device)) |
d550d98d | 593 | return -ENODEV; |
1da177e4 LT |
594 | acpi_device_dir(device)->owner = THIS_MODULE; |
595 | } | |
596 | ||
597 | /* 'info' [R] */ | |
598 | entry = create_proc_entry(ACPI_BATTERY_FILE_INFO, | |
4be44fcd | 599 | S_IRUGO, acpi_device_dir(device)); |
1da177e4 | 600 | if (!entry) |
d550d98d | 601 | return -ENODEV; |
1da177e4 | 602 | else { |
4be44fcd | 603 | entry->proc_fops = &acpi_battery_info_ops; |
1da177e4 LT |
604 | entry->data = acpi_driver_data(device); |
605 | entry->owner = THIS_MODULE; | |
606 | } | |
607 | ||
608 | /* 'status' [R] */ | |
609 | entry = create_proc_entry(ACPI_BATTERY_FILE_STATUS, | |
4be44fcd | 610 | S_IRUGO, acpi_device_dir(device)); |
1da177e4 | 611 | if (!entry) |
d550d98d | 612 | return -ENODEV; |
1da177e4 LT |
613 | else { |
614 | entry->proc_fops = &acpi_battery_state_ops; | |
615 | entry->data = acpi_driver_data(device); | |
616 | entry->owner = THIS_MODULE; | |
617 | } | |
618 | ||
619 | /* 'alarm' [R/W] */ | |
620 | entry = create_proc_entry(ACPI_BATTERY_FILE_ALARM, | |
4be44fcd LB |
621 | S_IFREG | S_IRUGO | S_IWUSR, |
622 | acpi_device_dir(device)); | |
1da177e4 | 623 | if (!entry) |
d550d98d | 624 | return -ENODEV; |
1da177e4 LT |
625 | else { |
626 | entry->proc_fops = &acpi_battery_alarm_ops; | |
627 | entry->data = acpi_driver_data(device); | |
628 | entry->owner = THIS_MODULE; | |
629 | } | |
630 | ||
d550d98d | 631 | return 0; |
1da177e4 LT |
632 | } |
633 | ||
4be44fcd | 634 | static int acpi_battery_remove_fs(struct acpi_device *device) |
1da177e4 | 635 | { |
1da177e4 LT |
636 | |
637 | if (acpi_device_dir(device)) { | |
638 | remove_proc_entry(ACPI_BATTERY_FILE_ALARM, | |
639 | acpi_device_dir(device)); | |
640 | remove_proc_entry(ACPI_BATTERY_FILE_STATUS, | |
641 | acpi_device_dir(device)); | |
642 | remove_proc_entry(ACPI_BATTERY_FILE_INFO, | |
643 | acpi_device_dir(device)); | |
644 | ||
645 | remove_proc_entry(acpi_device_bid(device), acpi_battery_dir); | |
646 | acpi_device_dir(device) = NULL; | |
647 | } | |
648 | ||
d550d98d | 649 | return 0; |
1da177e4 LT |
650 | } |
651 | ||
1da177e4 LT |
652 | /* -------------------------------------------------------------------------- |
653 | Driver Interface | |
654 | -------------------------------------------------------------------------- */ | |
655 | ||
4be44fcd | 656 | static void acpi_battery_notify(acpi_handle handle, u32 event, void *data) |
1da177e4 | 657 | { |
4be44fcd LB |
658 | struct acpi_battery *battery = (struct acpi_battery *)data; |
659 | struct acpi_device *device = NULL; | |
1da177e4 | 660 | |
1da177e4 LT |
661 | |
662 | if (!battery) | |
d550d98d | 663 | return; |
1da177e4 LT |
664 | |
665 | if (acpi_bus_get_device(handle, &device)) | |
d550d98d | 666 | return; |
1da177e4 LT |
667 | |
668 | switch (event) { | |
669 | case ACPI_BATTERY_NOTIFY_STATUS: | |
670 | case ACPI_BATTERY_NOTIFY_INFO: | |
671 | acpi_battery_check(battery); | |
672 | acpi_bus_generate_event(device, event, battery->flags.present); | |
673 | break; | |
674 | default: | |
675 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
4be44fcd | 676 | "Unsupported event [0x%x]\n", event)); |
1da177e4 LT |
677 | break; |
678 | } | |
679 | ||
d550d98d | 680 | return; |
1da177e4 LT |
681 | } |
682 | ||
4be44fcd | 683 | static int acpi_battery_add(struct acpi_device *device) |
1da177e4 | 684 | { |
4be44fcd LB |
685 | int result = 0; |
686 | acpi_status status = 0; | |
687 | struct acpi_battery *battery = NULL; | |
1da177e4 | 688 | |
4be44fcd | 689 | |
1da177e4 | 690 | if (!device) |
d550d98d | 691 | return -EINVAL; |
1da177e4 LT |
692 | |
693 | battery = kmalloc(sizeof(struct acpi_battery), GFP_KERNEL); | |
694 | if (!battery) | |
d550d98d | 695 | return -ENOMEM; |
1da177e4 LT |
696 | memset(battery, 0, sizeof(struct acpi_battery)); |
697 | ||
698 | battery->handle = device->handle; | |
699 | strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME); | |
700 | strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS); | |
701 | acpi_driver_data(device) = battery; | |
702 | ||
703 | result = acpi_battery_check(battery); | |
704 | if (result) | |
705 | goto end; | |
706 | ||
707 | result = acpi_battery_add_fs(device); | |
708 | if (result) | |
709 | goto end; | |
710 | ||
711 | status = acpi_install_notify_handler(battery->handle, | |
4be44fcd LB |
712 | ACPI_DEVICE_NOTIFY, |
713 | acpi_battery_notify, battery); | |
1da177e4 | 714 | if (ACPI_FAILURE(status)) { |
1da177e4 LT |
715 | result = -ENODEV; |
716 | goto end; | |
717 | } | |
718 | ||
719 | printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n", | |
4be44fcd LB |
720 | ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device), |
721 | device->status.battery_present ? "present" : "absent"); | |
722 | ||
723 | end: | |
1da177e4 LT |
724 | if (result) { |
725 | acpi_battery_remove_fs(device); | |
726 | kfree(battery); | |
727 | } | |
728 | ||
d550d98d | 729 | return result; |
1da177e4 LT |
730 | } |
731 | ||
4be44fcd | 732 | static int acpi_battery_remove(struct acpi_device *device, int type) |
1da177e4 | 733 | { |
4be44fcd LB |
734 | acpi_status status = 0; |
735 | struct acpi_battery *battery = NULL; | |
1da177e4 | 736 | |
1da177e4 LT |
737 | |
738 | if (!device || !acpi_driver_data(device)) | |
d550d98d | 739 | return -EINVAL; |
1da177e4 | 740 | |
4be44fcd | 741 | battery = (struct acpi_battery *)acpi_driver_data(device); |
1da177e4 LT |
742 | |
743 | status = acpi_remove_notify_handler(battery->handle, | |
4be44fcd LB |
744 | ACPI_DEVICE_NOTIFY, |
745 | acpi_battery_notify); | |
1da177e4 LT |
746 | |
747 | acpi_battery_remove_fs(device); | |
748 | ||
749 | kfree(battery); | |
750 | ||
d550d98d | 751 | return 0; |
1da177e4 LT |
752 | } |
753 | ||
4be44fcd | 754 | static int __init acpi_battery_init(void) |
1da177e4 | 755 | { |
4be44fcd | 756 | int result = 0; |
1da177e4 | 757 | |
1da177e4 LT |
758 | |
759 | acpi_battery_dir = proc_mkdir(ACPI_BATTERY_CLASS, acpi_root_dir); | |
760 | if (!acpi_battery_dir) | |
d550d98d | 761 | return -ENODEV; |
1da177e4 LT |
762 | acpi_battery_dir->owner = THIS_MODULE; |
763 | ||
764 | result = acpi_bus_register_driver(&acpi_battery_driver); | |
765 | if (result < 0) { | |
766 | remove_proc_entry(ACPI_BATTERY_CLASS, acpi_root_dir); | |
d550d98d | 767 | return -ENODEV; |
1da177e4 LT |
768 | } |
769 | ||
d550d98d | 770 | return 0; |
1da177e4 LT |
771 | } |
772 | ||
4be44fcd | 773 | static void __exit acpi_battery_exit(void) |
1da177e4 | 774 | { |
1da177e4 LT |
775 | |
776 | acpi_bus_unregister_driver(&acpi_battery_driver); | |
777 | ||
778 | remove_proc_entry(ACPI_BATTERY_CLASS, acpi_root_dir); | |
779 | ||
d550d98d | 780 | return; |
1da177e4 LT |
781 | } |
782 | ||
1da177e4 LT |
783 | module_init(acpi_battery_init); |
784 | module_exit(acpi_battery_exit); |