]>
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" | |
1da177e4 LT |
45 | #define ACPI_BATTERY_DEVICE_NAME "Battery" |
46 | #define ACPI_BATTERY_FILE_INFO "info" | |
a1f0eff2 | 47 | #define ACPI_BATTERY_FILE_STATE "state" |
1da177e4 LT |
48 | #define ACPI_BATTERY_FILE_ALARM "alarm" |
49 | #define ACPI_BATTERY_NOTIFY_STATUS 0x80 | |
50 | #define ACPI_BATTERY_NOTIFY_INFO 0x81 | |
51 | #define ACPI_BATTERY_UNITS_WATTS "mW" | |
52 | #define ACPI_BATTERY_UNITS_AMPS "mA" | |
53 | ||
1da177e4 | 54 | #define _COMPONENT ACPI_BATTERY_COMPONENT |
b6ce4083 VL |
55 | |
56 | #define ACPI_BATTERY_UPDATE_TIME 0 | |
57 | ||
58 | #define ACPI_BATTERY_NONE_UPDATE 0 | |
59 | #define ACPI_BATTERY_EASY_UPDATE 1 | |
60 | #define ACPI_BATTERY_INIT_UPDATE 2 | |
61 | ||
f52fd66d | 62 | ACPI_MODULE_NAME("battery"); |
1da177e4 | 63 | |
f52fd66d | 64 | MODULE_AUTHOR("Paul Diefenbaugh"); |
7cda93e0 | 65 | MODULE_DESCRIPTION("ACPI Battery Driver"); |
1da177e4 LT |
66 | MODULE_LICENSE("GPL"); |
67 | ||
b6ce4083 VL |
68 | static unsigned int update_time = ACPI_BATTERY_UPDATE_TIME; |
69 | ||
70 | /* 0 - every time, > 0 - by update_time */ | |
71 | module_param(update_time, uint, 0644); | |
72 | ||
3f86b832 RT |
73 | extern struct proc_dir_entry *acpi_lock_battery_dir(void); |
74 | extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir); | |
75 | ||
4be44fcd LB |
76 | static int acpi_battery_add(struct acpi_device *device); |
77 | static int acpi_battery_remove(struct acpi_device *device, int type); | |
5d9464a4 | 78 | static int acpi_battery_resume(struct acpi_device *device); |
1da177e4 LT |
79 | |
80 | static struct acpi_driver acpi_battery_driver = { | |
c2b6705b | 81 | .name = "battery", |
4be44fcd LB |
82 | .class = ACPI_BATTERY_CLASS, |
83 | .ids = ACPI_BATTERY_HID, | |
84 | .ops = { | |
85 | .add = acpi_battery_add, | |
34c4415a | 86 | .resume = acpi_battery_resume, |
4be44fcd LB |
87 | .remove = acpi_battery_remove, |
88 | }, | |
1da177e4 LT |
89 | }; |
90 | ||
a1f0eff2 | 91 | struct acpi_battery_state { |
4be44fcd LB |
92 | acpi_integer state; |
93 | acpi_integer present_rate; | |
94 | acpi_integer remaining_capacity; | |
95 | acpi_integer present_voltage; | |
1da177e4 LT |
96 | }; |
97 | ||
98 | struct acpi_battery_info { | |
4be44fcd LB |
99 | acpi_integer power_unit; |
100 | acpi_integer design_capacity; | |
101 | acpi_integer last_full_capacity; | |
102 | acpi_integer battery_technology; | |
103 | acpi_integer design_voltage; | |
104 | acpi_integer design_capacity_warning; | |
105 | acpi_integer design_capacity_low; | |
106 | acpi_integer battery_capacity_granularity_1; | |
107 | acpi_integer battery_capacity_granularity_2; | |
108 | acpi_string model_number; | |
109 | acpi_string serial_number; | |
110 | acpi_string battery_type; | |
111 | acpi_string oem_info; | |
1da177e4 LT |
112 | }; |
113 | ||
114 | struct acpi_battery_flags { | |
b6ce4083 VL |
115 | u8 battery_present_prev; |
116 | u8 alarm_present; | |
117 | u8 init_update; | |
118 | u8 info_update; | |
119 | u8 state_update; | |
120 | u8 alarm_update; | |
121 | u8 power_unit; | |
1da177e4 LT |
122 | }; |
123 | ||
124 | struct acpi_battery { | |
b6ce4083 | 125 | struct mutex mutex; |
145def84 | 126 | struct acpi_device * device; |
1da177e4 | 127 | struct acpi_battery_flags flags; |
b6ce4083 VL |
128 | struct acpi_buffer bif_data; |
129 | struct acpi_buffer bst_data; | |
4be44fcd | 130 | unsigned long alarm; |
b6ce4083 VL |
131 | unsigned long info_update_time; |
132 | unsigned long state_update_time; | |
133 | unsigned long alarm_update_time; | |
1da177e4 LT |
134 | }; |
135 | ||
b6ce4083 VL |
136 | #define acpi_battery_present(battery) battery->device->status.battery_present |
137 | #define acpi_battery_present_prev(battery) battery->flags.battery_present_prev | |
138 | #define acpi_battery_alarm_present(battery) battery->flags.alarm_present | |
139 | #define acpi_battery_init_update_flag(battery) battery->flags.init_update | |
140 | #define acpi_battery_info_update_flag(battery) battery->flags.info_update | |
141 | #define acpi_battery_state_update_flag(battery) battery->flags.state_update | |
142 | #define acpi_battery_alarm_update_flag(battery) battery->flags.alarm_update | |
143 | #define acpi_battery_power_units(battery) battery->flags.power_unit ? \ | |
144 | ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS | |
145 | #define acpi_battery_handle(battery) battery->device->handle | |
146 | #define acpi_battery_inserted(battery) (!acpi_battery_present_prev(battery) & acpi_battery_present(battery)) | |
147 | #define acpi_battery_removed(battery) (acpi_battery_present_prev(battery) & !acpi_battery_present(battery)) | |
148 | #define acpi_battery_bid(battery) acpi_device_bid(battery->device) | |
149 | #define acpi_battery_status_str(battery) acpi_battery_present(battery) ? "present" : "absent" | |
150 | ||
1da177e4 LT |
151 | /* -------------------------------------------------------------------------- |
152 | Battery Management | |
153 | -------------------------------------------------------------------------- */ | |
154 | ||
b6ce4083 VL |
155 | static void acpi_battery_mutex_lock(struct acpi_battery *battery) |
156 | { | |
157 | mutex_lock(&battery->mutex); | |
158 | } | |
159 | ||
160 | static void acpi_battery_mutex_unlock(struct acpi_battery *battery) | |
161 | { | |
162 | mutex_unlock(&battery->mutex); | |
163 | } | |
164 | ||
165 | static void acpi_battery_check_result(struct acpi_battery *battery, int result) | |
166 | { | |
167 | if (!battery) | |
168 | return; | |
169 | ||
170 | if (result) { | |
171 | acpi_battery_init_update_flag(battery) = 1; | |
172 | } | |
173 | } | |
174 | ||
175 | static int acpi_battery_extract_package(struct acpi_battery *battery, | |
176 | union acpi_object *package, | |
177 | struct acpi_buffer *format, | |
178 | struct acpi_buffer *data, | |
179 | char *package_name) | |
180 | { | |
181 | acpi_status status = AE_OK; | |
182 | struct acpi_buffer data_null = { 0, NULL }; | |
183 | ||
184 | status = acpi_extract_package(package, format, &data_null); | |
185 | if (status != AE_BUFFER_OVERFLOW) { | |
186 | ACPI_EXCEPTION((AE_INFO, status, "Extracting size %s", | |
187 | package_name)); | |
188 | return -ENODEV; | |
189 | } | |
190 | ||
191 | if (data_null.length != data->length) { | |
192 | if (data->pointer) { | |
193 | kfree(data->pointer); | |
194 | } | |
195 | data->pointer = kzalloc(data_null.length, GFP_KERNEL); | |
196 | if (!data->pointer) { | |
197 | ACPI_EXCEPTION((AE_INFO, AE_NO_MEMORY, "kzalloc()")); | |
198 | return -ENOMEM; | |
199 | } | |
200 | data->length = data_null.length; | |
201 | } | |
202 | ||
203 | status = acpi_extract_package(package, format, data); | |
204 | if (ACPI_FAILURE(status)) { | |
205 | ACPI_EXCEPTION((AE_INFO, status, "Extracting %s", | |
206 | package_name)); | |
207 | return -ENODEV; | |
208 | } | |
209 | ||
210 | return 0; | |
211 | } | |
212 | ||
213 | static int acpi_battery_get_status(struct acpi_battery *battery) | |
214 | { | |
215 | int result = 0; | |
216 | ||
217 | result = acpi_bus_get_status(battery->device); | |
218 | if (result) { | |
219 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA")); | |
220 | return -ENODEV; | |
221 | } | |
222 | return result; | |
223 | } | |
224 | ||
225 | static int acpi_battery_get_info(struct acpi_battery *battery) | |
1da177e4 | 226 | { |
4be44fcd LB |
227 | int result = 0; |
228 | acpi_status status = 0; | |
229 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
230 | struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BIF), | |
231 | ACPI_BATTERY_FORMAT_BIF | |
232 | }; | |
4be44fcd | 233 | union acpi_object *package = NULL; |
b6ce4083 VL |
234 | struct acpi_buffer *data = NULL; |
235 | struct acpi_battery_info *bif = NULL; | |
1da177e4 | 236 | |
b6ce4083 | 237 | battery->info_update_time = get_seconds(); |
1da177e4 | 238 | |
b6ce4083 VL |
239 | if (!acpi_battery_present(battery)) |
240 | return 0; | |
1da177e4 LT |
241 | |
242 | /* Evalute _BIF */ | |
243 | ||
b6ce4083 | 244 | status = acpi_evaluate_object(acpi_battery_handle(battery), "_BIF", NULL, &buffer); |
1da177e4 | 245 | if (ACPI_FAILURE(status)) { |
a6fc6720 | 246 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF")); |
d550d98d | 247 | return -ENODEV; |
1da177e4 LT |
248 | } |
249 | ||
50dd0969 | 250 | package = buffer.pointer; |
1da177e4 | 251 | |
b6ce4083 VL |
252 | data = &battery->bif_data; |
253 | ||
1da177e4 LT |
254 | /* Extract Package Data */ |
255 | ||
b6ce4083 VL |
256 | result = acpi_battery_extract_package(battery, package, &format, data, "_BIF"); |
257 | if (result) | |
1da177e4 | 258 | goto end; |
1da177e4 | 259 | |
b6ce4083 | 260 | end: |
1da177e4 | 261 | |
b6ce4083 VL |
262 | if (buffer.pointer) { |
263 | kfree(buffer.pointer); | |
1da177e4 LT |
264 | } |
265 | ||
b6ce4083 VL |
266 | if (!result) { |
267 | bif = data->pointer; | |
268 | battery->flags.power_unit = bif->power_unit; | |
269 | } | |
1da177e4 | 270 | |
d550d98d | 271 | return result; |
1da177e4 LT |
272 | } |
273 | ||
b6ce4083 | 274 | static int acpi_battery_get_state(struct acpi_battery *battery) |
1da177e4 | 275 | { |
4be44fcd LB |
276 | int result = 0; |
277 | acpi_status status = 0; | |
278 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
279 | struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BST), | |
280 | ACPI_BATTERY_FORMAT_BST | |
281 | }; | |
4be44fcd | 282 | union acpi_object *package = NULL; |
b6ce4083 | 283 | struct acpi_buffer *data = NULL; |
1da177e4 | 284 | |
b6ce4083 | 285 | battery->state_update_time = get_seconds(); |
1da177e4 | 286 | |
b6ce4083 VL |
287 | if (!acpi_battery_present(battery)) |
288 | return 0; | |
1da177e4 LT |
289 | |
290 | /* Evalute _BST */ | |
291 | ||
b6ce4083 | 292 | status = acpi_evaluate_object(acpi_battery_handle(battery), "_BST", NULL, &buffer); |
1da177e4 | 293 | if (ACPI_FAILURE(status)) { |
a6fc6720 | 294 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST")); |
d550d98d | 295 | return -ENODEV; |
1da177e4 LT |
296 | } |
297 | ||
50dd0969 | 298 | package = buffer.pointer; |
1da177e4 | 299 | |
b6ce4083 | 300 | data = &battery->bst_data; |
1da177e4 | 301 | |
b6ce4083 | 302 | /* Extract Package Data */ |
1da177e4 | 303 | |
b6ce4083 VL |
304 | result = acpi_battery_extract_package(battery, package, &format, data, "_BST"); |
305 | if (result) | |
1da177e4 | 306 | goto end; |
1da177e4 | 307 | |
b6ce4083 VL |
308 | end: |
309 | if (buffer.pointer) { | |
310 | kfree(buffer.pointer); | |
1da177e4 LT |
311 | } |
312 | ||
b6ce4083 VL |
313 | return result; |
314 | } | |
1da177e4 | 315 | |
b6ce4083 VL |
316 | static int acpi_battery_get_alarm(struct acpi_battery *battery) |
317 | { | |
318 | battery->alarm_update_time = get_seconds(); | |
1da177e4 | 319 | |
b6ce4083 | 320 | return 0; |
1da177e4 LT |
321 | } |
322 | ||
b6ce4083 | 323 | static int acpi_battery_set_alarm(struct acpi_battery *battery, unsigned long alarm) |
1da177e4 | 324 | { |
4be44fcd LB |
325 | acpi_status status = 0; |
326 | union acpi_object arg0 = { ACPI_TYPE_INTEGER }; | |
327 | struct acpi_object_list arg_list = { 1, &arg0 }; | |
1da177e4 | 328 | |
b6ce4083 | 329 | battery->alarm_update_time = get_seconds(); |
1da177e4 | 330 | |
b6ce4083 VL |
331 | if (!acpi_battery_present(battery)) |
332 | return -ENODEV; | |
1da177e4 | 333 | |
b6ce4083 | 334 | if (!acpi_battery_alarm_present(battery)) |
d550d98d | 335 | return -ENODEV; |
1da177e4 LT |
336 | |
337 | arg0.integer.value = alarm; | |
338 | ||
b6ce4083 | 339 | status = acpi_evaluate_object(acpi_battery_handle(battery), "_BTP", &arg_list, NULL); |
1da177e4 | 340 | if (ACPI_FAILURE(status)) |
d550d98d | 341 | return -ENODEV; |
1da177e4 LT |
342 | |
343 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", (u32) alarm)); | |
344 | ||
345 | battery->alarm = alarm; | |
346 | ||
d550d98d | 347 | return 0; |
1da177e4 LT |
348 | } |
349 | ||
b6ce4083 | 350 | static int acpi_battery_init_alarm(struct acpi_battery *battery) |
1da177e4 | 351 | { |
4be44fcd LB |
352 | int result = 0; |
353 | acpi_status status = AE_OK; | |
354 | acpi_handle handle = NULL; | |
b6ce4083 VL |
355 | struct acpi_battery_info *bif = battery->bif_data.pointer; |
356 | unsigned long alarm = battery->alarm; | |
4be44fcd | 357 | |
b6ce4083 | 358 | /* See if alarms are supported, and if so, set default */ |
1da177e4 | 359 | |
b6ce4083 VL |
360 | status = acpi_get_handle(acpi_battery_handle(battery), "_BTP", &handle); |
361 | if (ACPI_SUCCESS(status)) { | |
362 | acpi_battery_alarm_present(battery) = 1; | |
363 | if (!alarm && bif) { | |
364 | alarm = bif->design_capacity_warning; | |
365 | } | |
366 | result = acpi_battery_set_alarm(battery, alarm); | |
367 | if (result) | |
368 | goto end; | |
369 | } else { | |
370 | acpi_battery_alarm_present(battery) = 0; | |
371 | } | |
1da177e4 | 372 | |
b6ce4083 | 373 | end: |
1da177e4 | 374 | |
b6ce4083 VL |
375 | return result; |
376 | } | |
1da177e4 | 377 | |
b6ce4083 VL |
378 | static int acpi_battery_init_update(struct acpi_battery *battery) |
379 | { | |
380 | int result = 0; | |
1da177e4 | 381 | |
b6ce4083 VL |
382 | result = acpi_battery_get_status(battery); |
383 | if (result) | |
384 | return result; | |
1da177e4 | 385 | |
b6ce4083 | 386 | acpi_battery_present_prev(battery) = acpi_battery_present(battery); |
1da177e4 | 387 | |
b6ce4083 VL |
388 | if (acpi_battery_present(battery)) { |
389 | result = acpi_battery_get_info(battery); | |
390 | if (result) | |
391 | return result; | |
392 | result = acpi_battery_get_state(battery); | |
1da177e4 | 393 | if (result) |
d550d98d | 394 | return result; |
1da177e4 | 395 | |
b6ce4083 VL |
396 | acpi_battery_init_alarm(battery); |
397 | } | |
398 | ||
399 | return result; | |
400 | } | |
401 | ||
402 | static int acpi_battery_update(struct acpi_battery *battery, | |
403 | int update, int *update_result_ptr) | |
404 | { | |
405 | int result = 0; | |
406 | int update_result = ACPI_BATTERY_NONE_UPDATE; | |
1da177e4 | 407 | |
b6ce4083 VL |
408 | if (!acpi_battery_present(battery)) { |
409 | update = 1; | |
410 | } | |
1da177e4 | 411 | |
b6ce4083 VL |
412 | if (acpi_battery_init_update_flag(battery)) { |
413 | result = acpi_battery_init_update(battery); | |
414 | if (result) | |
415 | goto end;; | |
416 | update_result = ACPI_BATTERY_INIT_UPDATE; | |
417 | } else if (update) { | |
418 | result = acpi_battery_get_status(battery); | |
419 | if (result) | |
420 | goto end;; | |
421 | if (acpi_battery_inserted(battery) || acpi_battery_removed(battery)) { | |
422 | result = acpi_battery_init_update(battery); | |
423 | if (result) | |
424 | goto end;; | |
425 | update_result = ACPI_BATTERY_INIT_UPDATE; | |
426 | } else { | |
427 | update_result = ACPI_BATTERY_EASY_UPDATE; | |
1da177e4 LT |
428 | } |
429 | } | |
430 | ||
b6ce4083 | 431 | end: |
1da177e4 | 432 | |
b6ce4083 | 433 | acpi_battery_init_update_flag(battery) = (result != 0); |
1da177e4 | 434 | |
b6ce4083 | 435 | *update_result_ptr = update_result; |
1da177e4 | 436 | |
d550d98d | 437 | return result; |
1da177e4 LT |
438 | } |
439 | ||
b6ce4083 | 440 | static void acpi_battery_notify_update(struct acpi_battery *battery) |
4bd35cdb | 441 | { |
b6ce4083 VL |
442 | acpi_battery_get_status(battery); |
443 | ||
444 | if (acpi_battery_init_update_flag(battery)) { | |
445 | return; | |
446 | } | |
447 | ||
448 | if (acpi_battery_inserted(battery) || acpi_battery_removed(battery)) { | |
449 | acpi_battery_init_update_flag(battery) = 1; | |
450 | } else { | |
451 | acpi_battery_info_update_flag(battery) = 1; | |
452 | acpi_battery_state_update_flag(battery) = 1; | |
453 | acpi_battery_alarm_update_flag(battery) = 1; | |
4bd35cdb VL |
454 | } |
455 | } | |
456 | ||
1da177e4 LT |
457 | /* -------------------------------------------------------------------------- |
458 | FS Interface (/proc) | |
459 | -------------------------------------------------------------------------- */ | |
460 | ||
4be44fcd | 461 | static struct proc_dir_entry *acpi_battery_dir; |
b6ce4083 VL |
462 | |
463 | static int acpi_battery_read_info_print(struct seq_file *seq, int result) | |
1da177e4 | 464 | { |
50dd0969 | 465 | struct acpi_battery *battery = seq->private; |
1da177e4 | 466 | struct acpi_battery_info *bif = NULL; |
4be44fcd | 467 | char *units = "?"; |
1da177e4 | 468 | |
b6ce4083 | 469 | if (result) |
1da177e4 LT |
470 | goto end; |
471 | ||
b6ce4083 | 472 | if (acpi_battery_present(battery)) |
1da177e4 LT |
473 | seq_printf(seq, "present: yes\n"); |
474 | else { | |
475 | seq_printf(seq, "present: no\n"); | |
476 | goto end; | |
477 | } | |
478 | ||
b6ce4083 VL |
479 | bif = battery->bif_data.pointer; |
480 | if (!bif) { | |
481 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, "BIF buffer is NULL")); | |
482 | result = -ENODEV; | |
1da177e4 LT |
483 | goto end; |
484 | } | |
485 | ||
b6ce4083 VL |
486 | /* Battery Units */ |
487 | ||
488 | units = acpi_battery_power_units(battery); | |
4be44fcd | 489 | |
1da177e4 LT |
490 | if (bif->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
491 | seq_printf(seq, "design capacity: unknown\n"); | |
492 | else | |
493 | seq_printf(seq, "design capacity: %d %sh\n", | |
4be44fcd | 494 | (u32) bif->design_capacity, units); |
1da177e4 LT |
495 | |
496 | if (bif->last_full_capacity == ACPI_BATTERY_VALUE_UNKNOWN) | |
497 | seq_printf(seq, "last full capacity: unknown\n"); | |
498 | else | |
499 | seq_printf(seq, "last full capacity: %d %sh\n", | |
4be44fcd | 500 | (u32) bif->last_full_capacity, units); |
1da177e4 LT |
501 | |
502 | switch ((u32) bif->battery_technology) { | |
503 | case 0: | |
504 | seq_printf(seq, "battery technology: non-rechargeable\n"); | |
505 | break; | |
506 | case 1: | |
507 | seq_printf(seq, "battery technology: rechargeable\n"); | |
508 | break; | |
509 | default: | |
510 | seq_printf(seq, "battery technology: unknown\n"); | |
511 | break; | |
512 | } | |
513 | ||
514 | if (bif->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN) | |
515 | seq_printf(seq, "design voltage: unknown\n"); | |
516 | else | |
517 | seq_printf(seq, "design voltage: %d mV\n", | |
4be44fcd | 518 | (u32) bif->design_voltage); |
1da177e4 | 519 | seq_printf(seq, "design capacity warning: %d %sh\n", |
4be44fcd | 520 | (u32) bif->design_capacity_warning, units); |
1da177e4 | 521 | seq_printf(seq, "design capacity low: %d %sh\n", |
4be44fcd | 522 | (u32) bif->design_capacity_low, units); |
1da177e4 | 523 | seq_printf(seq, "capacity granularity 1: %d %sh\n", |
4be44fcd | 524 | (u32) bif->battery_capacity_granularity_1, units); |
1da177e4 | 525 | seq_printf(seq, "capacity granularity 2: %d %sh\n", |
4be44fcd LB |
526 | (u32) bif->battery_capacity_granularity_2, units); |
527 | seq_printf(seq, "model number: %s\n", bif->model_number); | |
528 | seq_printf(seq, "serial number: %s\n", bif->serial_number); | |
529 | seq_printf(seq, "battery type: %s\n", bif->battery_type); | |
530 | seq_printf(seq, "OEM info: %s\n", bif->oem_info); | |
531 | ||
532 | end: | |
1da177e4 | 533 | |
b6ce4083 VL |
534 | if (result) |
535 | seq_printf(seq, "ERROR: Unable to read battery info\n"); | |
536 | ||
537 | return result; | |
538 | } | |
539 | ||
540 | static int acpi_battery_read_info(struct seq_file *seq, void *offset) | |
541 | { | |
542 | struct acpi_battery *battery = seq->private; | |
543 | int result = 0; | |
544 | int update_result = ACPI_BATTERY_NONE_UPDATE; | |
545 | int update = 0; | |
546 | ||
547 | acpi_battery_mutex_lock(battery); | |
548 | ||
549 | update = (get_seconds() - battery->info_update_time >= update_time); | |
550 | update = (update | acpi_battery_info_update_flag(battery)); | |
551 | ||
552 | result = acpi_battery_update(battery, update, &update_result); | |
553 | if (result) | |
554 | goto end; | |
555 | ||
556 | /* Battery Info (_BIF) */ | |
557 | ||
558 | if (update_result == ACPI_BATTERY_EASY_UPDATE) { | |
559 | result = acpi_battery_get_info(battery); | |
560 | if (result) | |
561 | goto end; | |
562 | } | |
563 | ||
564 | end: | |
565 | ||
566 | result = acpi_battery_read_info_print(seq, result); | |
567 | ||
568 | acpi_battery_check_result(battery, result); | |
569 | ||
570 | acpi_battery_info_update_flag(battery) = result; | |
571 | ||
572 | acpi_battery_mutex_unlock(battery); | |
573 | ||
574 | return result; | |
1da177e4 LT |
575 | } |
576 | ||
577 | static int acpi_battery_info_open_fs(struct inode *inode, struct file *file) | |
578 | { | |
579 | return single_open(file, acpi_battery_read_info, PDE(inode)->data); | |
580 | } | |
581 | ||
b6ce4083 | 582 | static int acpi_battery_read_state_print(struct seq_file *seq, int result) |
1da177e4 | 583 | { |
50dd0969 | 584 | struct acpi_battery *battery = seq->private; |
a1f0eff2 | 585 | struct acpi_battery_state *bst = NULL; |
4be44fcd | 586 | char *units = "?"; |
1da177e4 | 587 | |
b6ce4083 | 588 | if (result) |
1da177e4 LT |
589 | goto end; |
590 | ||
b6ce4083 | 591 | if (acpi_battery_present(battery)) |
1da177e4 LT |
592 | seq_printf(seq, "present: yes\n"); |
593 | else { | |
594 | seq_printf(seq, "present: no\n"); | |
595 | goto end; | |
596 | } | |
597 | ||
b6ce4083 VL |
598 | bst = battery->bst_data.pointer; |
599 | if (!bst) { | |
600 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, "BST buffer is NULL")); | |
601 | result = -ENODEV; | |
1da177e4 LT |
602 | goto end; |
603 | } | |
604 | ||
b6ce4083 VL |
605 | /* Battery Units */ |
606 | ||
607 | units = acpi_battery_power_units(battery); | |
608 | ||
1da177e4 LT |
609 | if (!(bst->state & 0x04)) |
610 | seq_printf(seq, "capacity state: ok\n"); | |
611 | else | |
612 | seq_printf(seq, "capacity state: critical\n"); | |
613 | ||
4be44fcd LB |
614 | if ((bst->state & 0x01) && (bst->state & 0x02)) { |
615 | seq_printf(seq, | |
616 | "charging state: charging/discharging\n"); | |
4be44fcd | 617 | } else if (bst->state & 0x01) |
1da177e4 LT |
618 | seq_printf(seq, "charging state: discharging\n"); |
619 | else if (bst->state & 0x02) | |
620 | seq_printf(seq, "charging state: charging\n"); | |
621 | else { | |
622 | seq_printf(seq, "charging state: charged\n"); | |
623 | } | |
624 | ||
625 | if (bst->present_rate == ACPI_BATTERY_VALUE_UNKNOWN) | |
626 | seq_printf(seq, "present rate: unknown\n"); | |
627 | else | |
628 | seq_printf(seq, "present rate: %d %s\n", | |
4be44fcd | 629 | (u32) bst->present_rate, units); |
1da177e4 LT |
630 | |
631 | if (bst->remaining_capacity == ACPI_BATTERY_VALUE_UNKNOWN) | |
632 | seq_printf(seq, "remaining capacity: unknown\n"); | |
633 | else | |
634 | seq_printf(seq, "remaining capacity: %d %sh\n", | |
4be44fcd | 635 | (u32) bst->remaining_capacity, units); |
1da177e4 LT |
636 | |
637 | if (bst->present_voltage == ACPI_BATTERY_VALUE_UNKNOWN) | |
638 | seq_printf(seq, "present voltage: unknown\n"); | |
639 | else | |
640 | seq_printf(seq, "present voltage: %d mV\n", | |
4be44fcd | 641 | (u32) bst->present_voltage); |
1da177e4 | 642 | |
4be44fcd | 643 | end: |
1da177e4 | 644 | |
b6ce4083 VL |
645 | if (result) { |
646 | seq_printf(seq, "ERROR: Unable to read battery state\n"); | |
647 | } | |
648 | ||
649 | return result; | |
650 | } | |
651 | ||
652 | static int acpi_battery_read_state(struct seq_file *seq, void *offset) | |
653 | { | |
654 | struct acpi_battery *battery = seq->private; | |
655 | int result = 0; | |
656 | int update_result = ACPI_BATTERY_NONE_UPDATE; | |
657 | int update = 0; | |
658 | ||
659 | acpi_battery_mutex_lock(battery); | |
660 | ||
661 | update = (get_seconds() - battery->state_update_time >= update_time); | |
662 | update = (update | acpi_battery_state_update_flag(battery)); | |
663 | ||
664 | result = acpi_battery_update(battery, update, &update_result); | |
665 | if (result) | |
666 | goto end; | |
667 | ||
668 | /* Battery State (_BST) */ | |
669 | ||
670 | if (update_result == ACPI_BATTERY_EASY_UPDATE) { | |
671 | result = acpi_battery_get_state(battery); | |
672 | if (result) | |
673 | goto end; | |
674 | } | |
675 | ||
676 | end: | |
677 | ||
678 | result = acpi_battery_read_state_print(seq, result); | |
679 | ||
680 | acpi_battery_check_result(battery, result); | |
681 | ||
682 | acpi_battery_state_update_flag(battery) = result; | |
683 | ||
684 | acpi_battery_mutex_unlock(battery); | |
685 | ||
686 | return result; | |
1da177e4 LT |
687 | } |
688 | ||
689 | static int acpi_battery_state_open_fs(struct inode *inode, struct file *file) | |
690 | { | |
691 | return single_open(file, acpi_battery_read_state, PDE(inode)->data); | |
692 | } | |
693 | ||
b6ce4083 | 694 | static int acpi_battery_read_alarm_print(struct seq_file *seq, int result) |
1da177e4 | 695 | { |
50dd0969 | 696 | struct acpi_battery *battery = seq->private; |
4be44fcd | 697 | char *units = "?"; |
1da177e4 | 698 | |
b6ce4083 | 699 | if (result) |
1da177e4 LT |
700 | goto end; |
701 | ||
b6ce4083 | 702 | if (!acpi_battery_present(battery)) { |
1da177e4 LT |
703 | seq_printf(seq, "present: no\n"); |
704 | goto end; | |
705 | } | |
706 | ||
707 | /* Battery Units */ | |
4be44fcd | 708 | |
b6ce4083 | 709 | units = acpi_battery_power_units(battery); |
1da177e4 LT |
710 | |
711 | seq_printf(seq, "alarm: "); | |
712 | if (!battery->alarm) | |
713 | seq_printf(seq, "unsupported\n"); | |
714 | else | |
b6ce4083 | 715 | seq_printf(seq, "%lu %sh\n", battery->alarm, units); |
1da177e4 | 716 | |
4be44fcd | 717 | end: |
b6ce4083 VL |
718 | |
719 | if (result) | |
720 | seq_printf(seq, "ERROR: Unable to read battery alarm\n"); | |
721 | ||
722 | return result; | |
723 | } | |
724 | ||
725 | static int acpi_battery_read_alarm(struct seq_file *seq, void *offset) | |
726 | { | |
727 | struct acpi_battery *battery = seq->private; | |
728 | int result = 0; | |
729 | int update_result = ACPI_BATTERY_NONE_UPDATE; | |
730 | int update = 0; | |
731 | ||
732 | acpi_battery_mutex_lock(battery); | |
733 | ||
734 | update = (get_seconds() - battery->alarm_update_time >= update_time); | |
735 | update = (update | acpi_battery_alarm_update_flag(battery)); | |
736 | ||
737 | result = acpi_battery_update(battery, update, &update_result); | |
738 | if (result) | |
739 | goto end; | |
740 | ||
741 | /* Battery Alarm */ | |
742 | ||
743 | if (update_result == ACPI_BATTERY_EASY_UPDATE) { | |
744 | result = acpi_battery_get_alarm(battery); | |
745 | if (result) | |
746 | goto end; | |
747 | } | |
748 | ||
749 | end: | |
750 | ||
751 | result = acpi_battery_read_alarm_print(seq, result); | |
752 | ||
753 | acpi_battery_check_result(battery, result); | |
754 | ||
755 | acpi_battery_alarm_update_flag(battery) = result; | |
756 | ||
757 | acpi_battery_mutex_unlock(battery); | |
758 | ||
759 | return result; | |
1da177e4 LT |
760 | } |
761 | ||
1da177e4 | 762 | static ssize_t |
4be44fcd LB |
763 | acpi_battery_write_alarm(struct file *file, |
764 | const char __user * buffer, | |
765 | size_t count, loff_t * ppos) | |
1da177e4 | 766 | { |
4be44fcd LB |
767 | int result = 0; |
768 | char alarm_string[12] = { '\0' }; | |
50dd0969 JE |
769 | struct seq_file *m = file->private_data; |
770 | struct acpi_battery *battery = m->private; | |
b6ce4083 | 771 | int update_result = ACPI_BATTERY_NONE_UPDATE; |
1da177e4 | 772 | |
1da177e4 LT |
773 | |
774 | if (!battery || (count > sizeof(alarm_string) - 1)) | |
d550d98d | 775 | return -EINVAL; |
1da177e4 | 776 | |
b6ce4083 | 777 | acpi_battery_mutex_lock(battery); |
4bd35cdb | 778 | |
b6ce4083 VL |
779 | result = acpi_battery_update(battery, 1, &update_result); |
780 | if (result) { | |
781 | result = -ENODEV; | |
782 | goto end; | |
783 | } | |
1da177e4 | 784 | |
b6ce4083 VL |
785 | if (!acpi_battery_present(battery)) { |
786 | result = -ENODEV; | |
787 | goto end; | |
788 | } | |
789 | ||
790 | if (copy_from_user(alarm_string, buffer, count)) { | |
791 | result = -EFAULT; | |
792 | goto end; | |
793 | } | |
4be44fcd | 794 | |
1da177e4 LT |
795 | alarm_string[count] = '\0'; |
796 | ||
4be44fcd LB |
797 | result = acpi_battery_set_alarm(battery, |
798 | simple_strtoul(alarm_string, NULL, 0)); | |
1da177e4 | 799 | if (result) |
b6ce4083 VL |
800 | goto end; |
801 | ||
802 | end: | |
803 | ||
804 | acpi_battery_check_result(battery, result); | |
1da177e4 | 805 | |
b6ce4083 VL |
806 | if (!result) |
807 | result = count; | |
808 | ||
809 | acpi_battery_mutex_unlock(battery); | |
810 | ||
811 | return result; | |
1da177e4 LT |
812 | } |
813 | ||
814 | static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file) | |
815 | { | |
816 | return single_open(file, acpi_battery_read_alarm, PDE(inode)->data); | |
817 | } | |
818 | ||
d7508032 | 819 | static const struct file_operations acpi_battery_info_ops = { |
4be44fcd LB |
820 | .open = acpi_battery_info_open_fs, |
821 | .read = seq_read, | |
822 | .llseek = seq_lseek, | |
823 | .release = single_release, | |
1da177e4 LT |
824 | .owner = THIS_MODULE, |
825 | }; | |
826 | ||
d7508032 | 827 | static const struct file_operations acpi_battery_state_ops = { |
4be44fcd LB |
828 | .open = acpi_battery_state_open_fs, |
829 | .read = seq_read, | |
830 | .llseek = seq_lseek, | |
831 | .release = single_release, | |
1da177e4 LT |
832 | .owner = THIS_MODULE, |
833 | }; | |
834 | ||
d7508032 | 835 | static const struct file_operations acpi_battery_alarm_ops = { |
4be44fcd LB |
836 | .open = acpi_battery_alarm_open_fs, |
837 | .read = seq_read, | |
838 | .write = acpi_battery_write_alarm, | |
839 | .llseek = seq_lseek, | |
840 | .release = single_release, | |
1da177e4 LT |
841 | .owner = THIS_MODULE, |
842 | }; | |
843 | ||
4be44fcd | 844 | static int acpi_battery_add_fs(struct acpi_device *device) |
1da177e4 | 845 | { |
4be44fcd | 846 | struct proc_dir_entry *entry = NULL; |
1da177e4 | 847 | |
1da177e4 LT |
848 | |
849 | if (!acpi_device_dir(device)) { | |
850 | acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), | |
4be44fcd | 851 | acpi_battery_dir); |
1da177e4 | 852 | if (!acpi_device_dir(device)) |
d550d98d | 853 | return -ENODEV; |
1da177e4 LT |
854 | acpi_device_dir(device)->owner = THIS_MODULE; |
855 | } | |
856 | ||
857 | /* 'info' [R] */ | |
858 | entry = create_proc_entry(ACPI_BATTERY_FILE_INFO, | |
4be44fcd | 859 | S_IRUGO, acpi_device_dir(device)); |
1da177e4 | 860 | if (!entry) |
d550d98d | 861 | return -ENODEV; |
1da177e4 | 862 | else { |
4be44fcd | 863 | entry->proc_fops = &acpi_battery_info_ops; |
1da177e4 LT |
864 | entry->data = acpi_driver_data(device); |
865 | entry->owner = THIS_MODULE; | |
866 | } | |
867 | ||
868 | /* 'status' [R] */ | |
a1f0eff2 | 869 | entry = create_proc_entry(ACPI_BATTERY_FILE_STATE, |
4be44fcd | 870 | S_IRUGO, acpi_device_dir(device)); |
1da177e4 | 871 | if (!entry) |
d550d98d | 872 | return -ENODEV; |
1da177e4 LT |
873 | else { |
874 | entry->proc_fops = &acpi_battery_state_ops; | |
875 | entry->data = acpi_driver_data(device); | |
876 | entry->owner = THIS_MODULE; | |
877 | } | |
878 | ||
879 | /* 'alarm' [R/W] */ | |
880 | entry = create_proc_entry(ACPI_BATTERY_FILE_ALARM, | |
4be44fcd LB |
881 | S_IFREG | S_IRUGO | S_IWUSR, |
882 | acpi_device_dir(device)); | |
1da177e4 | 883 | if (!entry) |
d550d98d | 884 | return -ENODEV; |
1da177e4 LT |
885 | else { |
886 | entry->proc_fops = &acpi_battery_alarm_ops; | |
887 | entry->data = acpi_driver_data(device); | |
888 | entry->owner = THIS_MODULE; | |
889 | } | |
890 | ||
d550d98d | 891 | return 0; |
1da177e4 LT |
892 | } |
893 | ||
4be44fcd | 894 | static int acpi_battery_remove_fs(struct acpi_device *device) |
1da177e4 | 895 | { |
1da177e4 LT |
896 | if (acpi_device_dir(device)) { |
897 | remove_proc_entry(ACPI_BATTERY_FILE_ALARM, | |
898 | acpi_device_dir(device)); | |
a1f0eff2 | 899 | remove_proc_entry(ACPI_BATTERY_FILE_STATE, |
1da177e4 LT |
900 | acpi_device_dir(device)); |
901 | remove_proc_entry(ACPI_BATTERY_FILE_INFO, | |
902 | acpi_device_dir(device)); | |
903 | ||
904 | remove_proc_entry(acpi_device_bid(device), acpi_battery_dir); | |
905 | acpi_device_dir(device) = NULL; | |
906 | } | |
907 | ||
d550d98d | 908 | return 0; |
1da177e4 LT |
909 | } |
910 | ||
1da177e4 LT |
911 | /* -------------------------------------------------------------------------- |
912 | Driver Interface | |
913 | -------------------------------------------------------------------------- */ | |
914 | ||
4be44fcd | 915 | static void acpi_battery_notify(acpi_handle handle, u32 event, void *data) |
1da177e4 | 916 | { |
50dd0969 | 917 | struct acpi_battery *battery = data; |
4be44fcd | 918 | struct acpi_device *device = NULL; |
1da177e4 | 919 | |
1da177e4 LT |
920 | |
921 | if (!battery) | |
d550d98d | 922 | return; |
1da177e4 | 923 | |
145def84 | 924 | device = battery->device; |
1da177e4 LT |
925 | |
926 | switch (event) { | |
927 | case ACPI_BATTERY_NOTIFY_STATUS: | |
928 | case ACPI_BATTERY_NOTIFY_INFO: | |
9fdae727 VL |
929 | case ACPI_NOTIFY_BUS_CHECK: |
930 | case ACPI_NOTIFY_DEVICE_CHECK: | |
b6ce4083 VL |
931 | acpi_battery_mutex_lock(battery); |
932 | device = battery->device; | |
933 | acpi_battery_notify_update(battery); | |
934 | acpi_battery_mutex_unlock(battery); | |
935 | acpi_bus_generate_event(device, event, acpi_battery_present(battery)); | |
1da177e4 LT |
936 | break; |
937 | default: | |
938 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
4be44fcd | 939 | "Unsupported event [0x%x]\n", event)); |
1da177e4 LT |
940 | break; |
941 | } | |
942 | ||
d550d98d | 943 | return; |
1da177e4 LT |
944 | } |
945 | ||
4be44fcd | 946 | static int acpi_battery_add(struct acpi_device *device) |
1da177e4 | 947 | { |
4be44fcd LB |
948 | int result = 0; |
949 | acpi_status status = 0; | |
950 | struct acpi_battery *battery = NULL; | |
1da177e4 | 951 | |
1da177e4 | 952 | if (!device) |
d550d98d | 953 | return -EINVAL; |
1da177e4 | 954 | |
36bcbec7 | 955 | battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL); |
1da177e4 | 956 | if (!battery) |
d550d98d | 957 | return -ENOMEM; |
1da177e4 | 958 | |
b6ce4083 VL |
959 | mutex_init(&battery->mutex); |
960 | ||
961 | acpi_battery_mutex_lock(battery); | |
962 | ||
145def84 | 963 | battery->device = device; |
1da177e4 LT |
964 | strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME); |
965 | strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS); | |
966 | acpi_driver_data(device) = battery; | |
967 | ||
b6ce4083 | 968 | result = acpi_battery_get_status(battery); |
1da177e4 LT |
969 | if (result) |
970 | goto end; | |
971 | ||
b6ce4083 VL |
972 | acpi_battery_init_update_flag(battery) = 1; |
973 | ||
1da177e4 LT |
974 | result = acpi_battery_add_fs(device); |
975 | if (result) | |
976 | goto end; | |
977 | ||
3b073ec3 | 978 | status = acpi_install_notify_handler(device->handle, |
9fdae727 | 979 | ACPI_ALL_NOTIFY, |
4be44fcd | 980 | acpi_battery_notify, battery); |
1da177e4 | 981 | if (ACPI_FAILURE(status)) { |
b6ce4083 | 982 | ACPI_EXCEPTION((AE_INFO, status, "Installing notify handler")); |
1da177e4 LT |
983 | result = -ENODEV; |
984 | goto end; | |
985 | } | |
986 | ||
987 | printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n", | |
4be44fcd LB |
988 | ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device), |
989 | device->status.battery_present ? "present" : "absent"); | |
990 | ||
991 | end: | |
b6ce4083 | 992 | |
1da177e4 LT |
993 | if (result) { |
994 | acpi_battery_remove_fs(device); | |
995 | kfree(battery); | |
996 | } | |
997 | ||
b6ce4083 VL |
998 | acpi_battery_mutex_unlock(battery); |
999 | ||
d550d98d | 1000 | return result; |
1da177e4 LT |
1001 | } |
1002 | ||
4be44fcd | 1003 | static int acpi_battery_remove(struct acpi_device *device, int type) |
1da177e4 | 1004 | { |
4be44fcd LB |
1005 | acpi_status status = 0; |
1006 | struct acpi_battery *battery = NULL; | |
1da177e4 | 1007 | |
1da177e4 | 1008 | if (!device || !acpi_driver_data(device)) |
d550d98d | 1009 | return -EINVAL; |
1da177e4 | 1010 | |
50dd0969 | 1011 | battery = acpi_driver_data(device); |
1da177e4 | 1012 | |
b6ce4083 VL |
1013 | acpi_battery_mutex_lock(battery); |
1014 | ||
3b073ec3 | 1015 | status = acpi_remove_notify_handler(device->handle, |
9fdae727 | 1016 | ACPI_ALL_NOTIFY, |
4be44fcd | 1017 | acpi_battery_notify); |
1da177e4 LT |
1018 | |
1019 | acpi_battery_remove_fs(device); | |
1020 | ||
b6ce4083 VL |
1021 | if (battery->bif_data.pointer) |
1022 | kfree(battery->bif_data.pointer); | |
1023 | ||
1024 | if (battery->bst_data.pointer) | |
1025 | kfree(battery->bst_data.pointer); | |
1026 | ||
1027 | acpi_battery_mutex_unlock(battery); | |
1028 | ||
1029 | mutex_destroy(&battery->mutex); | |
1030 | ||
1da177e4 LT |
1031 | kfree(battery); |
1032 | ||
d550d98d | 1033 | return 0; |
1da177e4 LT |
1034 | } |
1035 | ||
34c4415a | 1036 | /* this is needed to learn about changes made in suspended state */ |
5d9464a4 | 1037 | static int acpi_battery_resume(struct acpi_device *device) |
34c4415a JK |
1038 | { |
1039 | struct acpi_battery *battery; | |
1040 | ||
1041 | if (!device) | |
1042 | return -EINVAL; | |
1043 | ||
1044 | battery = device->driver_data; | |
b6ce4083 VL |
1045 | |
1046 | acpi_battery_init_update_flag(battery) = 1; | |
1047 | ||
1048 | return 0; | |
34c4415a JK |
1049 | } |
1050 | ||
4be44fcd | 1051 | static int __init acpi_battery_init(void) |
1da177e4 | 1052 | { |
3f86b832 | 1053 | int result; |
1da177e4 | 1054 | |
4d8316d5 PM |
1055 | if (acpi_disabled) |
1056 | return -ENODEV; | |
1057 | ||
3f86b832 | 1058 | acpi_battery_dir = acpi_lock_battery_dir(); |
1da177e4 | 1059 | if (!acpi_battery_dir) |
d550d98d | 1060 | return -ENODEV; |
1da177e4 LT |
1061 | |
1062 | result = acpi_bus_register_driver(&acpi_battery_driver); | |
1063 | if (result < 0) { | |
3f86b832 | 1064 | acpi_unlock_battery_dir(acpi_battery_dir); |
d550d98d | 1065 | return -ENODEV; |
1da177e4 LT |
1066 | } |
1067 | ||
d550d98d | 1068 | return 0; |
1da177e4 LT |
1069 | } |
1070 | ||
4be44fcd | 1071 | static void __exit acpi_battery_exit(void) |
1da177e4 | 1072 | { |
1da177e4 LT |
1073 | acpi_bus_unregister_driver(&acpi_battery_driver); |
1074 | ||
3f86b832 | 1075 | acpi_unlock_battery_dir(acpi_battery_dir); |
1da177e4 | 1076 | |
d550d98d | 1077 | return; |
1da177e4 LT |
1078 | } |
1079 | ||
1da177e4 LT |
1080 | module_init(acpi_battery_init); |
1081 | module_exit(acpi_battery_exit); |