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