]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/acpi/battery.c
Merge branch 'for-john' of git://git.kernel.org/pub/scm/linux/kernel/git/jberg/mac80211
[mirror_ubuntu-zesty-kernel.git] / drivers / acpi / battery.c
1 /*
2 * battery.c - ACPI Battery Driver (Revision: 2.0)
3 *
4 * Copyright (C) 2007 Alexey Starikovskiy <astarikovskiy@suse.de>
5 * Copyright (C) 2004-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com>
6 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
7 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
8 *
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or (at
14 * your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24 *
25 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 */
27
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/types.h>
32 #include <linux/jiffies.h>
33 #include <linux/async.h>
34 #include <linux/dmi.h>
35 #include <linux/slab.h>
36 #include <linux/suspend.h>
37
38 #ifdef CONFIG_ACPI_PROCFS_POWER
39 #include <linux/proc_fs.h>
40 #include <linux/seq_file.h>
41 #include <asm/uaccess.h>
42 #endif
43
44 #include <acpi/acpi_bus.h>
45 #include <acpi/acpi_drivers.h>
46 #include <linux/power_supply.h>
47
48 #define PREFIX "ACPI: "
49
50 #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
51
52 #define ACPI_BATTERY_CLASS "battery"
53 #define ACPI_BATTERY_DEVICE_NAME "Battery"
54 #define ACPI_BATTERY_NOTIFY_STATUS 0x80
55 #define ACPI_BATTERY_NOTIFY_INFO 0x81
56 #define ACPI_BATTERY_NOTIFY_THRESHOLD 0x82
57
58 /* Battery power unit: 0 means mW, 1 means mA */
59 #define ACPI_BATTERY_POWER_UNIT_MA 1
60
61 #define _COMPONENT ACPI_BATTERY_COMPONENT
62
63 ACPI_MODULE_NAME("battery");
64
65 MODULE_AUTHOR("Paul Diefenbaugh");
66 MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
67 MODULE_DESCRIPTION("ACPI Battery Driver");
68 MODULE_LICENSE("GPL");
69
70 static unsigned int cache_time = 1000;
71 module_param(cache_time, uint, 0644);
72 MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
73
74 #ifdef CONFIG_ACPI_PROCFS_POWER
75 extern struct proc_dir_entry *acpi_lock_battery_dir(void);
76 extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
77
78 enum acpi_battery_files {
79 info_tag = 0,
80 state_tag,
81 alarm_tag,
82 ACPI_BATTERY_NUMFILES,
83 };
84
85 #endif
86
87 static const struct acpi_device_id battery_device_ids[] = {
88 {"PNP0C0A", 0},
89 {"", 0},
90 };
91
92 MODULE_DEVICE_TABLE(acpi, battery_device_ids);
93
94 enum {
95 ACPI_BATTERY_ALARM_PRESENT,
96 ACPI_BATTERY_XINFO_PRESENT,
97 ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY,
98 };
99
100 struct acpi_battery {
101 struct mutex lock;
102 struct mutex sysfs_lock;
103 struct power_supply bat;
104 struct acpi_device *device;
105 struct notifier_block pm_nb;
106 unsigned long update_time;
107 int rate_now;
108 int capacity_now;
109 int voltage_now;
110 int design_capacity;
111 int full_charge_capacity;
112 int technology;
113 int design_voltage;
114 int design_capacity_warning;
115 int design_capacity_low;
116 int cycle_count;
117 int measurement_accuracy;
118 int max_sampling_time;
119 int min_sampling_time;
120 int max_averaging_interval;
121 int min_averaging_interval;
122 int capacity_granularity_1;
123 int capacity_granularity_2;
124 int alarm;
125 char model_number[32];
126 char serial_number[32];
127 char type[32];
128 char oem_info[32];
129 int state;
130 int power_unit;
131 unsigned long flags;
132 };
133
134 #define to_acpi_battery(x) container_of(x, struct acpi_battery, bat)
135
136 inline int acpi_battery_present(struct acpi_battery *battery)
137 {
138 return battery->device->status.battery_present;
139 }
140
141 static int acpi_battery_technology(struct acpi_battery *battery)
142 {
143 if (!strcasecmp("NiCd", battery->type))
144 return POWER_SUPPLY_TECHNOLOGY_NiCd;
145 if (!strcasecmp("NiMH", battery->type))
146 return POWER_SUPPLY_TECHNOLOGY_NiMH;
147 if (!strcasecmp("LION", battery->type))
148 return POWER_SUPPLY_TECHNOLOGY_LION;
149 if (!strncasecmp("LI-ION", battery->type, 6))
150 return POWER_SUPPLY_TECHNOLOGY_LION;
151 if (!strcasecmp("LiP", battery->type))
152 return POWER_SUPPLY_TECHNOLOGY_LIPO;
153 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
154 }
155
156 static int acpi_battery_get_state(struct acpi_battery *battery);
157
158 static int acpi_battery_is_charged(struct acpi_battery *battery)
159 {
160 /* either charging or discharging */
161 if (battery->state != 0)
162 return 0;
163
164 /* battery not reporting charge */
165 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
166 battery->capacity_now == 0)
167 return 0;
168
169 /* good batteries update full_charge as the batteries degrade */
170 if (battery->full_charge_capacity == battery->capacity_now)
171 return 1;
172
173 /* fallback to using design values for broken batteries */
174 if (battery->design_capacity == battery->capacity_now)
175 return 1;
176
177 /* we don't do any sort of metric based on percentages */
178 return 0;
179 }
180
181 static int acpi_battery_get_property(struct power_supply *psy,
182 enum power_supply_property psp,
183 union power_supply_propval *val)
184 {
185 int ret = 0;
186 struct acpi_battery *battery = to_acpi_battery(psy);
187
188 if (acpi_battery_present(battery)) {
189 /* run battery update only if it is present */
190 acpi_battery_get_state(battery);
191 } else if (psp != POWER_SUPPLY_PROP_PRESENT)
192 return -ENODEV;
193 switch (psp) {
194 case POWER_SUPPLY_PROP_STATUS:
195 if (battery->state & 0x01)
196 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
197 else if (battery->state & 0x02)
198 val->intval = POWER_SUPPLY_STATUS_CHARGING;
199 else if (acpi_battery_is_charged(battery))
200 val->intval = POWER_SUPPLY_STATUS_FULL;
201 else
202 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
203 break;
204 case POWER_SUPPLY_PROP_PRESENT:
205 val->intval = acpi_battery_present(battery);
206 break;
207 case POWER_SUPPLY_PROP_TECHNOLOGY:
208 val->intval = acpi_battery_technology(battery);
209 break;
210 case POWER_SUPPLY_PROP_CYCLE_COUNT:
211 val->intval = battery->cycle_count;
212 break;
213 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
214 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
215 ret = -ENODEV;
216 else
217 val->intval = battery->design_voltage * 1000;
218 break;
219 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
220 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
221 ret = -ENODEV;
222 else
223 val->intval = battery->voltage_now * 1000;
224 break;
225 case POWER_SUPPLY_PROP_CURRENT_NOW:
226 case POWER_SUPPLY_PROP_POWER_NOW:
227 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
228 ret = -ENODEV;
229 else
230 val->intval = battery->rate_now * 1000;
231 break;
232 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
233 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
234 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
235 ret = -ENODEV;
236 else
237 val->intval = battery->design_capacity * 1000;
238 break;
239 case POWER_SUPPLY_PROP_CHARGE_FULL:
240 case POWER_SUPPLY_PROP_ENERGY_FULL:
241 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
242 ret = -ENODEV;
243 else
244 val->intval = battery->full_charge_capacity * 1000;
245 break;
246 case POWER_SUPPLY_PROP_CHARGE_NOW:
247 case POWER_SUPPLY_PROP_ENERGY_NOW:
248 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
249 ret = -ENODEV;
250 else
251 val->intval = battery->capacity_now * 1000;
252 break;
253 case POWER_SUPPLY_PROP_CAPACITY:
254 if (battery->capacity_now && battery->full_charge_capacity)
255 val->intval = battery->capacity_now * 100/
256 battery->full_charge_capacity;
257 else
258 val->intval = 0;
259 break;
260 case POWER_SUPPLY_PROP_MODEL_NAME:
261 val->strval = battery->model_number;
262 break;
263 case POWER_SUPPLY_PROP_MANUFACTURER:
264 val->strval = battery->oem_info;
265 break;
266 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
267 val->strval = battery->serial_number;
268 break;
269 default:
270 ret = -EINVAL;
271 }
272 return ret;
273 }
274
275 static enum power_supply_property charge_battery_props[] = {
276 POWER_SUPPLY_PROP_STATUS,
277 POWER_SUPPLY_PROP_PRESENT,
278 POWER_SUPPLY_PROP_TECHNOLOGY,
279 POWER_SUPPLY_PROP_CYCLE_COUNT,
280 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
281 POWER_SUPPLY_PROP_VOLTAGE_NOW,
282 POWER_SUPPLY_PROP_CURRENT_NOW,
283 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
284 POWER_SUPPLY_PROP_CHARGE_FULL,
285 POWER_SUPPLY_PROP_CHARGE_NOW,
286 POWER_SUPPLY_PROP_CAPACITY,
287 POWER_SUPPLY_PROP_MODEL_NAME,
288 POWER_SUPPLY_PROP_MANUFACTURER,
289 POWER_SUPPLY_PROP_SERIAL_NUMBER,
290 };
291
292 static enum power_supply_property energy_battery_props[] = {
293 POWER_SUPPLY_PROP_STATUS,
294 POWER_SUPPLY_PROP_PRESENT,
295 POWER_SUPPLY_PROP_TECHNOLOGY,
296 POWER_SUPPLY_PROP_CYCLE_COUNT,
297 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
298 POWER_SUPPLY_PROP_VOLTAGE_NOW,
299 POWER_SUPPLY_PROP_POWER_NOW,
300 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
301 POWER_SUPPLY_PROP_ENERGY_FULL,
302 POWER_SUPPLY_PROP_ENERGY_NOW,
303 POWER_SUPPLY_PROP_CAPACITY,
304 POWER_SUPPLY_PROP_MODEL_NAME,
305 POWER_SUPPLY_PROP_MANUFACTURER,
306 POWER_SUPPLY_PROP_SERIAL_NUMBER,
307 };
308
309 #ifdef CONFIG_ACPI_PROCFS_POWER
310 inline char *acpi_battery_units(struct acpi_battery *battery)
311 {
312 return (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) ?
313 "mA" : "mW";
314 }
315 #endif
316
317 /* --------------------------------------------------------------------------
318 Battery Management
319 -------------------------------------------------------------------------- */
320 struct acpi_offsets {
321 size_t offset; /* offset inside struct acpi_sbs_battery */
322 u8 mode; /* int or string? */
323 };
324
325 static struct acpi_offsets state_offsets[] = {
326 {offsetof(struct acpi_battery, state), 0},
327 {offsetof(struct acpi_battery, rate_now), 0},
328 {offsetof(struct acpi_battery, capacity_now), 0},
329 {offsetof(struct acpi_battery, voltage_now), 0},
330 };
331
332 static struct acpi_offsets info_offsets[] = {
333 {offsetof(struct acpi_battery, power_unit), 0},
334 {offsetof(struct acpi_battery, design_capacity), 0},
335 {offsetof(struct acpi_battery, full_charge_capacity), 0},
336 {offsetof(struct acpi_battery, technology), 0},
337 {offsetof(struct acpi_battery, design_voltage), 0},
338 {offsetof(struct acpi_battery, design_capacity_warning), 0},
339 {offsetof(struct acpi_battery, design_capacity_low), 0},
340 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
341 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
342 {offsetof(struct acpi_battery, model_number), 1},
343 {offsetof(struct acpi_battery, serial_number), 1},
344 {offsetof(struct acpi_battery, type), 1},
345 {offsetof(struct acpi_battery, oem_info), 1},
346 };
347
348 static struct acpi_offsets extended_info_offsets[] = {
349 {offsetof(struct acpi_battery, power_unit), 0},
350 {offsetof(struct acpi_battery, design_capacity), 0},
351 {offsetof(struct acpi_battery, full_charge_capacity), 0},
352 {offsetof(struct acpi_battery, technology), 0},
353 {offsetof(struct acpi_battery, design_voltage), 0},
354 {offsetof(struct acpi_battery, design_capacity_warning), 0},
355 {offsetof(struct acpi_battery, design_capacity_low), 0},
356 {offsetof(struct acpi_battery, cycle_count), 0},
357 {offsetof(struct acpi_battery, measurement_accuracy), 0},
358 {offsetof(struct acpi_battery, max_sampling_time), 0},
359 {offsetof(struct acpi_battery, min_sampling_time), 0},
360 {offsetof(struct acpi_battery, max_averaging_interval), 0},
361 {offsetof(struct acpi_battery, min_averaging_interval), 0},
362 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
363 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
364 {offsetof(struct acpi_battery, model_number), 1},
365 {offsetof(struct acpi_battery, serial_number), 1},
366 {offsetof(struct acpi_battery, type), 1},
367 {offsetof(struct acpi_battery, oem_info), 1},
368 };
369
370 static int extract_package(struct acpi_battery *battery,
371 union acpi_object *package,
372 struct acpi_offsets *offsets, int num)
373 {
374 int i;
375 union acpi_object *element;
376 if (package->type != ACPI_TYPE_PACKAGE)
377 return -EFAULT;
378 for (i = 0; i < num; ++i) {
379 if (package->package.count <= i)
380 return -EFAULT;
381 element = &package->package.elements[i];
382 if (offsets[i].mode) {
383 u8 *ptr = (u8 *)battery + offsets[i].offset;
384 if (element->type == ACPI_TYPE_STRING ||
385 element->type == ACPI_TYPE_BUFFER)
386 strncpy(ptr, element->string.pointer, 32);
387 else if (element->type == ACPI_TYPE_INTEGER) {
388 strncpy(ptr, (u8 *)&element->integer.value,
389 sizeof(u64));
390 ptr[sizeof(u64)] = 0;
391 } else
392 *ptr = 0; /* don't have value */
393 } else {
394 int *x = (int *)((u8 *)battery + offsets[i].offset);
395 *x = (element->type == ACPI_TYPE_INTEGER) ?
396 element->integer.value : -1;
397 }
398 }
399 return 0;
400 }
401
402 static int acpi_battery_get_status(struct acpi_battery *battery)
403 {
404 if (acpi_bus_get_status(battery->device)) {
405 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
406 return -ENODEV;
407 }
408 return 0;
409 }
410
411 static int acpi_battery_get_info(struct acpi_battery *battery)
412 {
413 int result = -EFAULT;
414 acpi_status status = 0;
415 char *name = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags)?
416 "_BIX" : "_BIF";
417
418 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
419
420 if (!acpi_battery_present(battery))
421 return 0;
422 mutex_lock(&battery->lock);
423 status = acpi_evaluate_object(battery->device->handle, name,
424 NULL, &buffer);
425 mutex_unlock(&battery->lock);
426
427 if (ACPI_FAILURE(status)) {
428 ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s", name));
429 return -ENODEV;
430 }
431 if (test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags))
432 result = extract_package(battery, buffer.pointer,
433 extended_info_offsets,
434 ARRAY_SIZE(extended_info_offsets));
435 else
436 result = extract_package(battery, buffer.pointer,
437 info_offsets, ARRAY_SIZE(info_offsets));
438 kfree(buffer.pointer);
439 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
440 battery->full_charge_capacity = battery->design_capacity;
441 return result;
442 }
443
444 static int acpi_battery_get_state(struct acpi_battery *battery)
445 {
446 int result = 0;
447 acpi_status status = 0;
448 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
449
450 if (!acpi_battery_present(battery))
451 return 0;
452
453 if (battery->update_time &&
454 time_before(jiffies, battery->update_time +
455 msecs_to_jiffies(cache_time)))
456 return 0;
457
458 mutex_lock(&battery->lock);
459 status = acpi_evaluate_object(battery->device->handle, "_BST",
460 NULL, &buffer);
461 mutex_unlock(&battery->lock);
462
463 if (ACPI_FAILURE(status)) {
464 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
465 return -ENODEV;
466 }
467
468 result = extract_package(battery, buffer.pointer,
469 state_offsets, ARRAY_SIZE(state_offsets));
470 battery->update_time = jiffies;
471 kfree(buffer.pointer);
472
473 /* For buggy DSDTs that report negative 16-bit values for either
474 * charging or discharging current and/or report 0 as 65536
475 * due to bad math.
476 */
477 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA &&
478 battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN &&
479 (s16)(battery->rate_now) < 0) {
480 battery->rate_now = abs((s16)battery->rate_now);
481 printk_once(KERN_WARNING FW_BUG "battery: (dis)charge rate"
482 " invalid.\n");
483 }
484
485 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
486 && battery->capacity_now >= 0 && battery->capacity_now <= 100)
487 battery->capacity_now = (battery->capacity_now *
488 battery->full_charge_capacity) / 100;
489 return result;
490 }
491
492 static int acpi_battery_set_alarm(struct acpi_battery *battery)
493 {
494 acpi_status status = 0;
495 union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER };
496 struct acpi_object_list arg_list = { 1, &arg0 };
497
498 if (!acpi_battery_present(battery) ||
499 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
500 return -ENODEV;
501
502 arg0.integer.value = battery->alarm;
503
504 mutex_lock(&battery->lock);
505 status = acpi_evaluate_object(battery->device->handle, "_BTP",
506 &arg_list, NULL);
507 mutex_unlock(&battery->lock);
508
509 if (ACPI_FAILURE(status))
510 return -ENODEV;
511
512 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
513 return 0;
514 }
515
516 static int acpi_battery_init_alarm(struct acpi_battery *battery)
517 {
518 acpi_status status = AE_OK;
519 acpi_handle handle = NULL;
520
521 /* See if alarms are supported, and if so, set default */
522 status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
523 if (ACPI_FAILURE(status)) {
524 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
525 return 0;
526 }
527 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
528 if (!battery->alarm)
529 battery->alarm = battery->design_capacity_warning;
530 return acpi_battery_set_alarm(battery);
531 }
532
533 static ssize_t acpi_battery_alarm_show(struct device *dev,
534 struct device_attribute *attr,
535 char *buf)
536 {
537 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
538 return sprintf(buf, "%d\n", battery->alarm * 1000);
539 }
540
541 static ssize_t acpi_battery_alarm_store(struct device *dev,
542 struct device_attribute *attr,
543 const char *buf, size_t count)
544 {
545 unsigned long x;
546 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
547 if (sscanf(buf, "%ld\n", &x) == 1)
548 battery->alarm = x/1000;
549 if (acpi_battery_present(battery))
550 acpi_battery_set_alarm(battery);
551 return count;
552 }
553
554 static struct device_attribute alarm_attr = {
555 .attr = {.name = "alarm", .mode = 0644},
556 .show = acpi_battery_alarm_show,
557 .store = acpi_battery_alarm_store,
558 };
559
560 static int sysfs_add_battery(struct acpi_battery *battery)
561 {
562 int result;
563
564 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) {
565 battery->bat.properties = charge_battery_props;
566 battery->bat.num_properties =
567 ARRAY_SIZE(charge_battery_props);
568 } else {
569 battery->bat.properties = energy_battery_props;
570 battery->bat.num_properties =
571 ARRAY_SIZE(energy_battery_props);
572 }
573
574 battery->bat.name = acpi_device_bid(battery->device);
575 battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
576 battery->bat.get_property = acpi_battery_get_property;
577
578 result = power_supply_register(&battery->device->dev, &battery->bat);
579 if (result)
580 return result;
581 return device_create_file(battery->bat.dev, &alarm_attr);
582 }
583
584 static void sysfs_remove_battery(struct acpi_battery *battery)
585 {
586 mutex_lock(&battery->sysfs_lock);
587 if (!battery->bat.dev) {
588 mutex_unlock(&battery->sysfs_lock);
589 return;
590 }
591
592 device_remove_file(battery->bat.dev, &alarm_attr);
593 power_supply_unregister(&battery->bat);
594 battery->bat.dev = NULL;
595 mutex_unlock(&battery->sysfs_lock);
596 }
597
598 /*
599 * According to the ACPI spec, some kinds of primary batteries can
600 * report percentage battery remaining capacity directly to OS.
601 * In this case, it reports the Last Full Charged Capacity == 100
602 * and BatteryPresentRate == 0xFFFFFFFF.
603 *
604 * Now we found some battery reports percentage remaining capacity
605 * even if it's rechargeable.
606 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
607 *
608 * Handle this correctly so that they won't break userspace.
609 */
610 static void acpi_battery_quirks(struct acpi_battery *battery)
611 {
612 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
613 return ;
614
615 if (battery->full_charge_capacity == 100 &&
616 battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
617 battery->capacity_now >=0 && battery->capacity_now <= 100) {
618 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
619 battery->full_charge_capacity = battery->design_capacity;
620 battery->capacity_now = (battery->capacity_now *
621 battery->full_charge_capacity) / 100;
622 }
623 }
624
625 static int acpi_battery_update(struct acpi_battery *battery)
626 {
627 int result, old_present = acpi_battery_present(battery);
628 result = acpi_battery_get_status(battery);
629 if (result)
630 return result;
631 if (!acpi_battery_present(battery)) {
632 sysfs_remove_battery(battery);
633 battery->update_time = 0;
634 return 0;
635 }
636 if (!battery->update_time ||
637 old_present != acpi_battery_present(battery)) {
638 result = acpi_battery_get_info(battery);
639 if (result)
640 return result;
641 acpi_battery_init_alarm(battery);
642 }
643 if (!battery->bat.dev) {
644 result = sysfs_add_battery(battery);
645 if (result)
646 return result;
647 }
648 result = acpi_battery_get_state(battery);
649 acpi_battery_quirks(battery);
650 return result;
651 }
652
653 static void acpi_battery_refresh(struct acpi_battery *battery)
654 {
655 int power_unit;
656
657 if (!battery->bat.dev)
658 return;
659
660 power_unit = battery->power_unit;
661
662 acpi_battery_get_info(battery);
663
664 if (power_unit == battery->power_unit)
665 return;
666
667 /* The battery has changed its reporting units. */
668 sysfs_remove_battery(battery);
669 sysfs_add_battery(battery);
670 }
671
672 /* --------------------------------------------------------------------------
673 FS Interface (/proc)
674 -------------------------------------------------------------------------- */
675
676 #ifdef CONFIG_ACPI_PROCFS_POWER
677 static struct proc_dir_entry *acpi_battery_dir;
678
679 static int acpi_battery_print_info(struct seq_file *seq, int result)
680 {
681 struct acpi_battery *battery = seq->private;
682
683 if (result)
684 goto end;
685
686 seq_printf(seq, "present: %s\n",
687 acpi_battery_present(battery)?"yes":"no");
688 if (!acpi_battery_present(battery))
689 goto end;
690 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
691 seq_printf(seq, "design capacity: unknown\n");
692 else
693 seq_printf(seq, "design capacity: %d %sh\n",
694 battery->design_capacity,
695 acpi_battery_units(battery));
696
697 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
698 seq_printf(seq, "last full capacity: unknown\n");
699 else
700 seq_printf(seq, "last full capacity: %d %sh\n",
701 battery->full_charge_capacity,
702 acpi_battery_units(battery));
703
704 seq_printf(seq, "battery technology: %srechargeable\n",
705 (!battery->technology)?"non-":"");
706
707 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
708 seq_printf(seq, "design voltage: unknown\n");
709 else
710 seq_printf(seq, "design voltage: %d mV\n",
711 battery->design_voltage);
712 seq_printf(seq, "design capacity warning: %d %sh\n",
713 battery->design_capacity_warning,
714 acpi_battery_units(battery));
715 seq_printf(seq, "design capacity low: %d %sh\n",
716 battery->design_capacity_low,
717 acpi_battery_units(battery));
718 seq_printf(seq, "cycle count: %i\n", battery->cycle_count);
719 seq_printf(seq, "capacity granularity 1: %d %sh\n",
720 battery->capacity_granularity_1,
721 acpi_battery_units(battery));
722 seq_printf(seq, "capacity granularity 2: %d %sh\n",
723 battery->capacity_granularity_2,
724 acpi_battery_units(battery));
725 seq_printf(seq, "model number: %s\n", battery->model_number);
726 seq_printf(seq, "serial number: %s\n", battery->serial_number);
727 seq_printf(seq, "battery type: %s\n", battery->type);
728 seq_printf(seq, "OEM info: %s\n", battery->oem_info);
729 end:
730 if (result)
731 seq_printf(seq, "ERROR: Unable to read battery info\n");
732 return result;
733 }
734
735 static int acpi_battery_print_state(struct seq_file *seq, int result)
736 {
737 struct acpi_battery *battery = seq->private;
738
739 if (result)
740 goto end;
741
742 seq_printf(seq, "present: %s\n",
743 acpi_battery_present(battery)?"yes":"no");
744 if (!acpi_battery_present(battery))
745 goto end;
746
747 seq_printf(seq, "capacity state: %s\n",
748 (battery->state & 0x04)?"critical":"ok");
749 if ((battery->state & 0x01) && (battery->state & 0x02))
750 seq_printf(seq,
751 "charging state: charging/discharging\n");
752 else if (battery->state & 0x01)
753 seq_printf(seq, "charging state: discharging\n");
754 else if (battery->state & 0x02)
755 seq_printf(seq, "charging state: charging\n");
756 else
757 seq_printf(seq, "charging state: charged\n");
758
759 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
760 seq_printf(seq, "present rate: unknown\n");
761 else
762 seq_printf(seq, "present rate: %d %s\n",
763 battery->rate_now, acpi_battery_units(battery));
764
765 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
766 seq_printf(seq, "remaining capacity: unknown\n");
767 else
768 seq_printf(seq, "remaining capacity: %d %sh\n",
769 battery->capacity_now, acpi_battery_units(battery));
770 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
771 seq_printf(seq, "present voltage: unknown\n");
772 else
773 seq_printf(seq, "present voltage: %d mV\n",
774 battery->voltage_now);
775 end:
776 if (result)
777 seq_printf(seq, "ERROR: Unable to read battery state\n");
778
779 return result;
780 }
781
782 static int acpi_battery_print_alarm(struct seq_file *seq, int result)
783 {
784 struct acpi_battery *battery = seq->private;
785
786 if (result)
787 goto end;
788
789 if (!acpi_battery_present(battery)) {
790 seq_printf(seq, "present: no\n");
791 goto end;
792 }
793 seq_printf(seq, "alarm: ");
794 if (!battery->alarm)
795 seq_printf(seq, "unsupported\n");
796 else
797 seq_printf(seq, "%u %sh\n", battery->alarm,
798 acpi_battery_units(battery));
799 end:
800 if (result)
801 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
802 return result;
803 }
804
805 static ssize_t acpi_battery_write_alarm(struct file *file,
806 const char __user * buffer,
807 size_t count, loff_t * ppos)
808 {
809 int result = 0;
810 char alarm_string[12] = { '\0' };
811 struct seq_file *m = file->private_data;
812 struct acpi_battery *battery = m->private;
813
814 if (!battery || (count > sizeof(alarm_string) - 1))
815 return -EINVAL;
816 if (!acpi_battery_present(battery)) {
817 result = -ENODEV;
818 goto end;
819 }
820 if (copy_from_user(alarm_string, buffer, count)) {
821 result = -EFAULT;
822 goto end;
823 }
824 alarm_string[count] = '\0';
825 battery->alarm = simple_strtol(alarm_string, NULL, 0);
826 result = acpi_battery_set_alarm(battery);
827 end:
828 if (!result)
829 return count;
830 return result;
831 }
832
833 typedef int(*print_func)(struct seq_file *seq, int result);
834
835 static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
836 acpi_battery_print_info,
837 acpi_battery_print_state,
838 acpi_battery_print_alarm,
839 };
840
841 static int acpi_battery_read(int fid, struct seq_file *seq)
842 {
843 struct acpi_battery *battery = seq->private;
844 int result = acpi_battery_update(battery);
845 return acpi_print_funcs[fid](seq, result);
846 }
847
848 #define DECLARE_FILE_FUNCTIONS(_name) \
849 static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
850 { \
851 return acpi_battery_read(_name##_tag, seq); \
852 } \
853 static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
854 { \
855 return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
856 }
857
858 DECLARE_FILE_FUNCTIONS(info);
859 DECLARE_FILE_FUNCTIONS(state);
860 DECLARE_FILE_FUNCTIONS(alarm);
861
862 #undef DECLARE_FILE_FUNCTIONS
863
864 #define FILE_DESCRIPTION_RO(_name) \
865 { \
866 .name = __stringify(_name), \
867 .mode = S_IRUGO, \
868 .ops = { \
869 .open = acpi_battery_##_name##_open_fs, \
870 .read = seq_read, \
871 .llseek = seq_lseek, \
872 .release = single_release, \
873 .owner = THIS_MODULE, \
874 }, \
875 }
876
877 #define FILE_DESCRIPTION_RW(_name) \
878 { \
879 .name = __stringify(_name), \
880 .mode = S_IFREG | S_IRUGO | S_IWUSR, \
881 .ops = { \
882 .open = acpi_battery_##_name##_open_fs, \
883 .read = seq_read, \
884 .llseek = seq_lseek, \
885 .write = acpi_battery_write_##_name, \
886 .release = single_release, \
887 .owner = THIS_MODULE, \
888 }, \
889 }
890
891 static const struct battery_file {
892 struct file_operations ops;
893 umode_t mode;
894 const char *name;
895 } acpi_battery_file[] = {
896 FILE_DESCRIPTION_RO(info),
897 FILE_DESCRIPTION_RO(state),
898 FILE_DESCRIPTION_RW(alarm),
899 };
900
901 #undef FILE_DESCRIPTION_RO
902 #undef FILE_DESCRIPTION_RW
903
904 static int acpi_battery_add_fs(struct acpi_device *device)
905 {
906 struct proc_dir_entry *entry = NULL;
907 int i;
908
909 printk(KERN_WARNING PREFIX "Deprecated procfs I/F for battery is loaded,"
910 " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
911 if (!acpi_device_dir(device)) {
912 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
913 acpi_battery_dir);
914 if (!acpi_device_dir(device))
915 return -ENODEV;
916 }
917
918 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
919 entry = proc_create_data(acpi_battery_file[i].name,
920 acpi_battery_file[i].mode,
921 acpi_device_dir(device),
922 &acpi_battery_file[i].ops,
923 acpi_driver_data(device));
924 if (!entry)
925 return -ENODEV;
926 }
927 return 0;
928 }
929
930 static void acpi_battery_remove_fs(struct acpi_device *device)
931 {
932 int i;
933 if (!acpi_device_dir(device))
934 return;
935 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
936 remove_proc_entry(acpi_battery_file[i].name,
937 acpi_device_dir(device));
938
939 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
940 acpi_device_dir(device) = NULL;
941 }
942
943 #endif
944
945 /* --------------------------------------------------------------------------
946 Driver Interface
947 -------------------------------------------------------------------------- */
948
949 static void acpi_battery_notify(struct acpi_device *device, u32 event)
950 {
951 struct acpi_battery *battery = acpi_driver_data(device);
952 struct device *old;
953
954 if (!battery)
955 return;
956 old = battery->bat.dev;
957 if (event == ACPI_BATTERY_NOTIFY_INFO)
958 acpi_battery_refresh(battery);
959 acpi_battery_update(battery);
960 acpi_bus_generate_proc_event(device, event,
961 acpi_battery_present(battery));
962 acpi_bus_generate_netlink_event(device->pnp.device_class,
963 dev_name(&device->dev), event,
964 acpi_battery_present(battery));
965 /* acpi_battery_update could remove power_supply object */
966 if (old && battery->bat.dev)
967 power_supply_changed(&battery->bat);
968 }
969
970 static int battery_notify(struct notifier_block *nb,
971 unsigned long mode, void *_unused)
972 {
973 struct acpi_battery *battery = container_of(nb, struct acpi_battery,
974 pm_nb);
975 switch (mode) {
976 case PM_POST_HIBERNATION:
977 case PM_POST_SUSPEND:
978 if (battery->bat.dev) {
979 sysfs_remove_battery(battery);
980 sysfs_add_battery(battery);
981 }
982 break;
983 }
984
985 return 0;
986 }
987
988 static int acpi_battery_add(struct acpi_device *device)
989 {
990 int result = 0;
991 struct acpi_battery *battery = NULL;
992 acpi_handle handle;
993 if (!device)
994 return -EINVAL;
995 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
996 if (!battery)
997 return -ENOMEM;
998 battery->device = device;
999 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
1000 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
1001 device->driver_data = battery;
1002 mutex_init(&battery->lock);
1003 mutex_init(&battery->sysfs_lock);
1004 if (ACPI_SUCCESS(acpi_get_handle(battery->device->handle,
1005 "_BIX", &handle)))
1006 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
1007 result = acpi_battery_update(battery);
1008 if (result)
1009 goto fail;
1010 #ifdef CONFIG_ACPI_PROCFS_POWER
1011 result = acpi_battery_add_fs(device);
1012 #endif
1013 if (result) {
1014 #ifdef CONFIG_ACPI_PROCFS_POWER
1015 acpi_battery_remove_fs(device);
1016 #endif
1017 goto fail;
1018 }
1019
1020 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
1021 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
1022 device->status.battery_present ? "present" : "absent");
1023
1024 battery->pm_nb.notifier_call = battery_notify;
1025 register_pm_notifier(&battery->pm_nb);
1026
1027 return result;
1028
1029 fail:
1030 sysfs_remove_battery(battery);
1031 mutex_destroy(&battery->lock);
1032 mutex_destroy(&battery->sysfs_lock);
1033 kfree(battery);
1034 return result;
1035 }
1036
1037 static int acpi_battery_remove(struct acpi_device *device, int type)
1038 {
1039 struct acpi_battery *battery = NULL;
1040
1041 if (!device || !acpi_driver_data(device))
1042 return -EINVAL;
1043 battery = acpi_driver_data(device);
1044 unregister_pm_notifier(&battery->pm_nb);
1045 #ifdef CONFIG_ACPI_PROCFS_POWER
1046 acpi_battery_remove_fs(device);
1047 #endif
1048 sysfs_remove_battery(battery);
1049 mutex_destroy(&battery->lock);
1050 mutex_destroy(&battery->sysfs_lock);
1051 kfree(battery);
1052 return 0;
1053 }
1054
1055 #ifdef CONFIG_PM_SLEEP
1056 /* this is needed to learn about changes made in suspended state */
1057 static int acpi_battery_resume(struct device *dev)
1058 {
1059 struct acpi_battery *battery;
1060
1061 if (!dev)
1062 return -EINVAL;
1063
1064 battery = acpi_driver_data(to_acpi_device(dev));
1065 if (!battery)
1066 return -EINVAL;
1067
1068 battery->update_time = 0;
1069 acpi_battery_update(battery);
1070 return 0;
1071 }
1072 #endif
1073
1074 static SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume);
1075
1076 static struct acpi_driver acpi_battery_driver = {
1077 .name = "battery",
1078 .class = ACPI_BATTERY_CLASS,
1079 .ids = battery_device_ids,
1080 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1081 .ops = {
1082 .add = acpi_battery_add,
1083 .remove = acpi_battery_remove,
1084 .notify = acpi_battery_notify,
1085 },
1086 .drv.pm = &acpi_battery_pm,
1087 };
1088
1089 static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
1090 {
1091 if (acpi_disabled)
1092 return;
1093 #ifdef CONFIG_ACPI_PROCFS_POWER
1094 acpi_battery_dir = acpi_lock_battery_dir();
1095 if (!acpi_battery_dir)
1096 return;
1097 #endif
1098 if (acpi_bus_register_driver(&acpi_battery_driver) < 0) {
1099 #ifdef CONFIG_ACPI_PROCFS_POWER
1100 acpi_unlock_battery_dir(acpi_battery_dir);
1101 #endif
1102 return;
1103 }
1104 return;
1105 }
1106
1107 static int __init acpi_battery_init(void)
1108 {
1109 async_schedule(acpi_battery_init_async, NULL);
1110 return 0;
1111 }
1112
1113 static void __exit acpi_battery_exit(void)
1114 {
1115 acpi_bus_unregister_driver(&acpi_battery_driver);
1116 #ifdef CONFIG_ACPI_PROCFS_POWER
1117 acpi_unlock_battery_dir(acpi_battery_dir);
1118 #endif
1119 }
1120
1121 module_init(acpi_battery_init);
1122 module_exit(acpi_battery_exit);