1 /* 2 * acpi_sbs.c - ACPI Smart Battery System Driver ($Revision: 1.16 $) 3 * 4 * Copyright (c) 2005 Rich Townsend <rhdt@bartol.udel.edu> 5 * 6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or (at 11 * your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, but 14 * WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License along 19 * with this program; if not, write to the Free Software Foundation, Inc., 20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 21 * 22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 23 */ 24 25 #include <linux/init.h> 26 #include <linux/module.h> 27 #include <linux/moduleparam.h> 28 #include <linux/kernel.h> 29 #include <linux/proc_fs.h> 30 #include <linux/seq_file.h> 31 #include <asm/uaccess.h> 32 #include <linux/acpi.h> 33 #include <linux/timer.h> 34 #include <linux/jiffies.h> 35 #include <linux/delay.h> 36 37 #define ACPI_SBS_COMPONENT 0x00080000 38 #define ACPI_SBS_CLASS "sbs" 39 #define ACPI_AC_CLASS "ac_adapter" 40 #define ACPI_BATTERY_CLASS "battery" 41 #define ACPI_SBS_HID "ACPI0002" 42 #define ACPI_SBS_DEVICE_NAME "Smart Battery System" 43 #define ACPI_SBS_FILE_INFO "info" 44 #define ACPI_SBS_FILE_STATE "state" 45 #define ACPI_SBS_FILE_ALARM "alarm" 46 #define ACPI_BATTERY_DIR_NAME "BAT%i" 47 #define ACPI_AC_DIR_NAME "AC0" 48 #define ACPI_SBC_SMBUS_ADDR 0x9 49 #define ACPI_SBSM_SMBUS_ADDR 0xa 50 #define ACPI_SB_SMBUS_ADDR 0xb 51 #define ACPI_SBS_AC_NOTIFY_STATUS 0x80 52 #define ACPI_SBS_BATTERY_NOTIFY_STATUS 0x80 53 #define ACPI_SBS_BATTERY_NOTIFY_INFO 0x81 54 55 #define _COMPONENT ACPI_SBS_COMPONENT 56 57 ACPI_MODULE_NAME("sbs"); 58 59 MODULE_AUTHOR("Rich Townsend"); 60 MODULE_DESCRIPTION("Smart Battery System ACPI interface driver"); 61 MODULE_LICENSE("GPL"); 62 63 #define xmsleep(t) msleep(t) 64 65 #define ACPI_EC_SMB_PRTCL 0x00 /* protocol, PEC */ 66 67 #define ACPI_EC_SMB_STS 0x01 /* status */ 68 #define ACPI_EC_SMB_ADDR 0x02 /* address */ 69 #define ACPI_EC_SMB_CMD 0x03 /* command */ 70 #define ACPI_EC_SMB_DATA 0x04 /* 32 data registers */ 71 #define ACPI_EC_SMB_BCNT 0x24 /* number of data bytes */ 72 73 #define ACPI_EC_SMB_STS_DONE 0x80 74 #define ACPI_EC_SMB_STS_STATUS 0x1f 75 76 #define ACPI_EC_SMB_PRTCL_WRITE 0x00 77 #define ACPI_EC_SMB_PRTCL_READ 0x01 78 #define ACPI_EC_SMB_PRTCL_WORD_DATA 0x08 79 #define ACPI_EC_SMB_PRTCL_BLOCK_DATA 0x0a 80 81 #define ACPI_EC_SMB_TRANSACTION_SLEEP 1 82 #define ACPI_EC_SMB_ACCESS_SLEEP1 1 83 #define ACPI_EC_SMB_ACCESS_SLEEP2 10 84 85 #define DEF_CAPACITY_UNIT 3 86 #define MAH_CAPACITY_UNIT 1 87 #define MWH_CAPACITY_UNIT 2 88 #define CAPACITY_UNIT DEF_CAPACITY_UNIT 89 90 #define REQUEST_UPDATE_MODE 1 91 #define QUEUE_UPDATE_MODE 2 92 93 #define DATA_TYPE_COMMON 0 94 #define DATA_TYPE_INFO 1 95 #define DATA_TYPE_STATE 2 96 #define DATA_TYPE_ALARM 3 97 #define DATA_TYPE_AC_STATE 4 98 99 extern struct proc_dir_entry *acpi_lock_ac_dir(void); 100 extern struct proc_dir_entry *acpi_lock_battery_dir(void); 101 extern void acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir); 102 extern void acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir); 103 104 #define MAX_SBS_BAT 4 105 #define ACPI_SBS_BLOCK_MAX 32 106 107 #define ACPI_SBS_SMBUS_READ 1 108 #define ACPI_SBS_SMBUS_WRITE 2 109 110 #define ACPI_SBS_WORD_DATA 1 111 #define ACPI_SBS_BLOCK_DATA 2 112 113 #define UPDATE_DELAY 10 114 115 /* 0 - every time, > 0 - by update_time */ 116 static unsigned int update_time = 120; 117 118 static unsigned int capacity_mode = CAPACITY_UNIT; 119 120 module_param(update_time, uint, 0644); 121 module_param(capacity_mode, uint, 0444); 122 123 static int acpi_sbs_add(struct acpi_device *device); 124 static int acpi_sbs_remove(struct acpi_device *device, int type); 125 static int acpi_sbs_resume(struct acpi_device *device); 126 127 static struct acpi_driver acpi_sbs_driver = { 128 .name = "sbs", 129 .class = ACPI_SBS_CLASS, 130 .ids = ACPI_SBS_HID, 131 .ops = { 132 .add = acpi_sbs_add, 133 .remove = acpi_sbs_remove, 134 .resume = acpi_sbs_resume, 135 }, 136 }; 137 138 struct acpi_ac { 139 int ac_present; 140 }; 141 142 struct acpi_battery_info { 143 int capacity_mode; 144 s16 full_charge_capacity; 145 s16 design_capacity; 146 s16 design_voltage; 147 int vscale; 148 int ipscale; 149 s16 serial_number; 150 char manufacturer_name[ACPI_SBS_BLOCK_MAX + 3]; 151 char device_name[ACPI_SBS_BLOCK_MAX + 3]; 152 char device_chemistry[ACPI_SBS_BLOCK_MAX + 3]; 153 }; 154 155 struct acpi_battery_state { 156 s16 voltage; 157 s16 amperage; 158 s16 remaining_capacity; 159 s16 battery_state; 160 }; 161 162 struct acpi_battery_alarm { 163 s16 remaining_capacity; 164 }; 165 166 struct acpi_battery { 167 int alive; 168 int id; 169 int init_state; 170 int battery_present; 171 struct acpi_sbs *sbs; 172 struct acpi_battery_info info; 173 struct acpi_battery_state state; 174 struct acpi_battery_alarm alarm; 175 struct proc_dir_entry *battery_entry; 176 }; 177 178 struct acpi_sbs { 179 acpi_handle handle; 180 int base; 181 struct acpi_device *device; 182 struct acpi_ec_smbus *smbus; 183 struct mutex mutex; 184 int sbsm_present; 185 int sbsm_batteries_supported; 186 struct proc_dir_entry *ac_entry; 187 struct acpi_ac ac; 188 struct acpi_battery battery[MAX_SBS_BAT]; 189 int zombie; 190 struct timer_list update_timer; 191 int run_cnt; 192 int update_proc_flg; 193 }; 194 195 static int acpi_sbs_update_run(struct acpi_sbs *sbs, int id, int data_type); 196 static void acpi_sbs_update_time(void *data); 197 198 union sbs_rw_data { 199 u16 word; 200 u8 block[ACPI_SBS_BLOCK_MAX + 2]; 201 }; 202 203 static int acpi_ec_sbs_access(struct acpi_sbs *sbs, u16 addr, 204 char read_write, u8 command, int size, 205 union sbs_rw_data *data); 206 207 /* -------------------------------------------------------------------------- 208 SMBus Communication 209 -------------------------------------------------------------------------- */ 210 211 static int acpi_ec_sbs_read(struct acpi_sbs *sbs, u8 address, u8 * data) 212 { 213 u8 val; 214 int err; 215 216 err = ec_read(sbs->base + address, &val); 217 if (!err) { 218 *data = val; 219 } 220 xmsleep(ACPI_EC_SMB_TRANSACTION_SLEEP); 221 return (err); 222 } 223 224 static int acpi_ec_sbs_write(struct acpi_sbs *sbs, u8 address, u8 data) 225 { 226 int err; 227 228 err = ec_write(sbs->base + address, data); 229 return (err); 230 } 231 232 static int 233 acpi_ec_sbs_access(struct acpi_sbs *sbs, u16 addr, 234 char read_write, u8 command, int size, 235 union sbs_rw_data *data) 236 { 237 unsigned char protocol, len = 0, temp[2] = { 0, 0 }; 238 int i; 239 240 if (read_write == ACPI_SBS_SMBUS_READ) { 241 protocol = ACPI_EC_SMB_PRTCL_READ; 242 } else { 243 protocol = ACPI_EC_SMB_PRTCL_WRITE; 244 } 245 246 switch (size) { 247 248 case ACPI_SBS_WORD_DATA: 249 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_CMD, command); 250 if (read_write == ACPI_SBS_SMBUS_WRITE) { 251 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_DATA, data->word); 252 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_DATA + 1, 253 data->word >> 8); 254 } 255 protocol |= ACPI_EC_SMB_PRTCL_WORD_DATA; 256 break; 257 case ACPI_SBS_BLOCK_DATA: 258 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_CMD, command); 259 if (read_write == ACPI_SBS_SMBUS_WRITE) { 260 len = min_t(u8, data->block[0], 32); 261 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_BCNT, len); 262 for (i = 0; i < len; i++) 263 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_DATA + i, 264 data->block[i + 1]); 265 } 266 protocol |= ACPI_EC_SMB_PRTCL_BLOCK_DATA; 267 break; 268 default: 269 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 270 "unsupported transaction %d", size)); 271 return (-1); 272 } 273 274 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_ADDR, addr << 1); 275 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_PRTCL, protocol); 276 277 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_STS, temp); 278 279 if (~temp[0] & ACPI_EC_SMB_STS_DONE) { 280 xmsleep(ACPI_EC_SMB_ACCESS_SLEEP1); 281 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_STS, temp); 282 } 283 if (~temp[0] & ACPI_EC_SMB_STS_DONE) { 284 xmsleep(ACPI_EC_SMB_ACCESS_SLEEP2); 285 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_STS, temp); 286 } 287 if ((~temp[0] & ACPI_EC_SMB_STS_DONE) 288 || (temp[0] & ACPI_EC_SMB_STS_STATUS)) { 289 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 290 "transaction %d error", size)); 291 return (-1); 292 } 293 294 if (read_write == ACPI_SBS_SMBUS_WRITE) { 295 return (0); 296 } 297 298 switch (size) { 299 300 case ACPI_SBS_WORD_DATA: 301 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_DATA, temp); 302 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_DATA + 1, temp + 1); 303 data->word = (temp[1] << 8) | temp[0]; 304 break; 305 306 case ACPI_SBS_BLOCK_DATA: 307 len = 0; 308 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_BCNT, &len); 309 len = min_t(u8, len, 32); 310 for (i = 0; i < len; i++) 311 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_DATA + i, 312 data->block + i + 1); 313 data->block[0] = len; 314 break; 315 default: 316 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 317 "unsupported transaction %d", size)); 318 return (-1); 319 } 320 321 return (0); 322 } 323 324 static int 325 acpi_sbs_read_word(struct acpi_sbs *sbs, int addr, int func, u16 * word) 326 { 327 union sbs_rw_data data; 328 int result = 0; 329 330 result = acpi_ec_sbs_access(sbs, addr, 331 ACPI_SBS_SMBUS_READ, func, 332 ACPI_SBS_WORD_DATA, &data); 333 if (result) { 334 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 335 "acpi_ec_sbs_access() failed")); 336 } else { 337 *word = data.word; 338 } 339 340 return result; 341 } 342 343 static int 344 acpi_sbs_read_str(struct acpi_sbs *sbs, int addr, int func, char *str) 345 { 346 union sbs_rw_data data; 347 int result = 0; 348 349 result = acpi_ec_sbs_access(sbs, addr, 350 ACPI_SBS_SMBUS_READ, func, 351 ACPI_SBS_BLOCK_DATA, &data); 352 if (result) { 353 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 354 "acpi_ec_sbs_access() failed")); 355 } else { 356 strncpy(str, (const char *)data.block + 1, data.block[0]); 357 str[data.block[0]] = 0; 358 } 359 360 return result; 361 } 362 363 static int 364 acpi_sbs_write_word(struct acpi_sbs *sbs, int addr, int func, int word) 365 { 366 union sbs_rw_data data; 367 int result = 0; 368 369 data.word = word; 370 371 result = acpi_ec_sbs_access(sbs, addr, 372 ACPI_SBS_SMBUS_WRITE, func, 373 ACPI_SBS_WORD_DATA, &data); 374 if (result) { 375 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 376 "acpi_ec_sbs_access() failed")); 377 } 378 379 return result; 380 } 381 382 static int sbs_zombie(struct acpi_sbs *sbs) 383 { 384 return (sbs->zombie); 385 } 386 387 static int sbs_mutex_lock(struct acpi_sbs *sbs) 388 { 389 if (sbs_zombie(sbs)) { 390 return -ENODEV; 391 } 392 mutex_lock(&sbs->mutex); 393 return 0; 394 } 395 396 static void sbs_mutex_unlock(struct acpi_sbs *sbs) 397 { 398 mutex_unlock(&sbs->mutex); 399 } 400 401 /* -------------------------------------------------------------------------- 402 Smart Battery System Management 403 -------------------------------------------------------------------------- */ 404 405 static int acpi_check_update_proc(struct acpi_sbs *sbs) 406 { 407 acpi_status status = AE_OK; 408 409 if (update_time == 0) { 410 sbs->update_proc_flg = 0; 411 return 0; 412 } 413 if (sbs->update_proc_flg == 0) { 414 status = acpi_os_execute(OSL_GPE_HANDLER, 415 acpi_sbs_update_time, sbs); 416 if (status != AE_OK) { 417 ACPI_EXCEPTION((AE_INFO, status, 418 "acpi_os_execute() failed")); 419 return 1; 420 } 421 sbs->update_proc_flg = 1; 422 } 423 return 0; 424 } 425 426 static int acpi_sbs_generate_event(struct acpi_device *device, 427 int event, int state, char *bid, char *class) 428 { 429 char bid_saved[5]; 430 char class_saved[20]; 431 int result = 0; 432 433 strcpy(bid_saved, acpi_device_bid(device)); 434 strcpy(class_saved, acpi_device_class(device)); 435 436 strcpy(acpi_device_bid(device), bid); 437 strcpy(acpi_device_class(device), class); 438 439 result = acpi_bus_generate_event(device, event, state); 440 441 strcpy(acpi_device_bid(device), bid_saved); 442 strcpy(acpi_device_class(device), class_saved); 443 444 return result; 445 } 446 447 static int acpi_battery_get_present(struct acpi_battery *battery) 448 { 449 s16 state; 450 int result = 0; 451 int is_present = 0; 452 453 result = acpi_sbs_read_word(battery->sbs, 454 ACPI_SBSM_SMBUS_ADDR, 0x01, &state); 455 if (result) { 456 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 457 "acpi_sbs_read_word() failed")); 458 } 459 if (!result) { 460 is_present = (state & 0x000f) & (1 << battery->id); 461 } 462 battery->battery_present = is_present; 463 464 return result; 465 } 466 467 static int acpi_battery_select(struct acpi_battery *battery) 468 { 469 struct acpi_sbs *sbs = battery->sbs; 470 int result = 0; 471 s16 state; 472 int foo; 473 474 if (sbs->sbsm_present) { 475 476 /* Take special care not to knobble other nibbles of 477 * state (aka selector_state), since 478 * it causes charging to halt on SBSELs */ 479 480 result = 481 acpi_sbs_read_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x01, &state); 482 if (result) { 483 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 484 "acpi_sbs_read_word() failed")); 485 goto end; 486 } 487 488 foo = (state & 0x0fff) | (1 << (battery->id + 12)); 489 result = 490 acpi_sbs_write_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x01, foo); 491 if (result) { 492 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 493 "acpi_sbs_write_word() failed")); 494 goto end; 495 } 496 } 497 498 end: 499 return result; 500 } 501 502 static int acpi_sbsm_get_info(struct acpi_sbs *sbs) 503 { 504 int result = 0; 505 s16 battery_system_info; 506 507 result = acpi_sbs_read_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x04, 508 &battery_system_info); 509 if (result) { 510 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 511 "acpi_sbs_read_word() failed")); 512 goto end; 513 } 514 515 sbs->sbsm_batteries_supported = battery_system_info & 0x000f; 516 517 end: 518 519 return result; 520 } 521 522 static int acpi_battery_get_info(struct acpi_battery *battery) 523 { 524 struct acpi_sbs *sbs = battery->sbs; 525 int result = 0; 526 s16 battery_mode; 527 s16 specification_info; 528 529 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x03, 530 &battery_mode); 531 if (result) { 532 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 533 "acpi_sbs_read_word() failed")); 534 goto end; 535 } 536 battery->info.capacity_mode = (battery_mode & 0x8000) >> 15; 537 538 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x10, 539 &battery->info.full_charge_capacity); 540 if (result) { 541 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 542 "acpi_sbs_read_word() failed")); 543 goto end; 544 } 545 546 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x18, 547 &battery->info.design_capacity); 548 549 if (result) { 550 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 551 "acpi_sbs_read_word() failed")); 552 goto end; 553 } 554 555 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x19, 556 &battery->info.design_voltage); 557 if (result) { 558 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 559 "acpi_sbs_read_word() failed")); 560 goto end; 561 } 562 563 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1a, 564 &specification_info); 565 if (result) { 566 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 567 "acpi_sbs_read_word() failed")); 568 goto end; 569 } 570 571 switch ((specification_info & 0x0f00) >> 8) { 572 case 1: 573 battery->info.vscale = 10; 574 break; 575 case 2: 576 battery->info.vscale = 100; 577 break; 578 case 3: 579 battery->info.vscale = 1000; 580 break; 581 default: 582 battery->info.vscale = 1; 583 } 584 585 switch ((specification_info & 0xf000) >> 12) { 586 case 1: 587 battery->info.ipscale = 10; 588 break; 589 case 2: 590 battery->info.ipscale = 100; 591 break; 592 case 3: 593 battery->info.ipscale = 1000; 594 break; 595 default: 596 battery->info.ipscale = 1; 597 } 598 599 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1c, 600 &battery->info.serial_number); 601 if (result) { 602 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 603 "acpi_sbs_read_word() failed")); 604 goto end; 605 } 606 607 result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x20, 608 battery->info.manufacturer_name); 609 if (result) { 610 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 611 "acpi_sbs_read_str() failed")); 612 goto end; 613 } 614 615 result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x21, 616 battery->info.device_name); 617 if (result) { 618 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 619 "acpi_sbs_read_str() failed")); 620 goto end; 621 } 622 623 result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x22, 624 battery->info.device_chemistry); 625 if (result) { 626 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 627 "acpi_sbs_read_str() failed")); 628 goto end; 629 } 630 631 end: 632 return result; 633 } 634 635 static int acpi_battery_get_state(struct acpi_battery *battery) 636 { 637 struct acpi_sbs *sbs = battery->sbs; 638 int result = 0; 639 640 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x09, 641 &battery->state.voltage); 642 if (result) { 643 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 644 "acpi_sbs_read_word() failed")); 645 goto end; 646 } 647 648 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x0a, 649 &battery->state.amperage); 650 if (result) { 651 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 652 "acpi_sbs_read_word() failed")); 653 goto end; 654 } 655 656 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x0f, 657 &battery->state.remaining_capacity); 658 if (result) { 659 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 660 "acpi_sbs_read_word() failed")); 661 goto end; 662 } 663 664 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x16, 665 &battery->state.battery_state); 666 if (result) { 667 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 668 "acpi_sbs_read_word() failed")); 669 goto end; 670 } 671 672 end: 673 return result; 674 } 675 676 static int acpi_battery_get_alarm(struct acpi_battery *battery) 677 { 678 struct acpi_sbs *sbs = battery->sbs; 679 int result = 0; 680 681 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01, 682 &battery->alarm.remaining_capacity); 683 if (result) { 684 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 685 "acpi_sbs_read_word() failed")); 686 goto end; 687 } 688 689 end: 690 691 return result; 692 } 693 694 static int acpi_battery_set_alarm(struct acpi_battery *battery, 695 unsigned long alarm) 696 { 697 struct acpi_sbs *sbs = battery->sbs; 698 int result = 0; 699 s16 battery_mode; 700 int foo; 701 702 result = acpi_battery_select(battery); 703 if (result) { 704 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 705 "acpi_battery_select() failed")); 706 goto end; 707 } 708 709 /* If necessary, enable the alarm */ 710 711 if (alarm > 0) { 712 result = 713 acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x03, 714 &battery_mode); 715 if (result) { 716 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 717 "acpi_sbs_read_word() failed")); 718 goto end; 719 } 720 721 result = 722 acpi_sbs_write_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01, 723 battery_mode & 0xbfff); 724 if (result) { 725 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 726 "acpi_sbs_write_word() failed")); 727 goto end; 728 } 729 } 730 731 foo = alarm / (battery->info.capacity_mode ? 10 : 1); 732 result = acpi_sbs_write_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01, foo); 733 if (result) { 734 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 735 "acpi_sbs_write_word() failed")); 736 goto end; 737 } 738 739 end: 740 741 return result; 742 } 743 744 static int acpi_battery_set_mode(struct acpi_battery *battery) 745 { 746 struct acpi_sbs *sbs = battery->sbs; 747 int result = 0; 748 s16 battery_mode; 749 750 if (capacity_mode == DEF_CAPACITY_UNIT) { 751 goto end; 752 } 753 754 result = acpi_sbs_read_word(sbs, 755 ACPI_SB_SMBUS_ADDR, 0x03, &battery_mode); 756 if (result) { 757 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 758 "acpi_sbs_read_word() failed")); 759 goto end; 760 } 761 762 if (capacity_mode == MAH_CAPACITY_UNIT) { 763 battery_mode &= 0x7fff; 764 } else { 765 battery_mode |= 0x8000; 766 } 767 result = acpi_sbs_write_word(sbs, 768 ACPI_SB_SMBUS_ADDR, 0x03, battery_mode); 769 if (result) { 770 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 771 "acpi_sbs_write_word() failed")); 772 goto end; 773 } 774 775 result = acpi_sbs_read_word(sbs, 776 ACPI_SB_SMBUS_ADDR, 0x03, &battery_mode); 777 if (result) { 778 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 779 "acpi_sbs_read_word() failed")); 780 goto end; 781 } 782 783 end: 784 return result; 785 } 786 787 static int acpi_battery_init(struct acpi_battery *battery) 788 { 789 int result = 0; 790 791 result = acpi_battery_select(battery); 792 if (result) { 793 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 794 "acpi_battery_select() failed")); 795 goto end; 796 } 797 798 result = acpi_battery_set_mode(battery); 799 if (result) { 800 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 801 "acpi_battery_set_mode() failed")); 802 goto end; 803 } 804 805 result = acpi_battery_get_info(battery); 806 if (result) { 807 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 808 "acpi_battery_get_info() failed")); 809 goto end; 810 } 811 812 result = acpi_battery_get_state(battery); 813 if (result) { 814 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 815 "acpi_battery_get_state() failed")); 816 goto end; 817 } 818 819 result = acpi_battery_get_alarm(battery); 820 if (result) { 821 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 822 "acpi_battery_get_alarm() failed")); 823 goto end; 824 } 825 826 end: 827 return result; 828 } 829 830 static int acpi_ac_get_present(struct acpi_sbs *sbs) 831 { 832 int result = 0; 833 s16 charger_status; 834 835 result = acpi_sbs_read_word(sbs, ACPI_SBC_SMBUS_ADDR, 0x13, 836 &charger_status); 837 838 if (result) { 839 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 840 "acpi_sbs_read_word() failed")); 841 goto end; 842 } 843 844 sbs->ac.ac_present = (charger_status & 0x8000) >> 15; 845 846 end: 847 848 return result; 849 } 850 851 /* -------------------------------------------------------------------------- 852 FS Interface (/proc/acpi) 853 -------------------------------------------------------------------------- */ 854 855 /* Generic Routines */ 856 857 static int 858 acpi_sbs_generic_add_fs(struct proc_dir_entry **dir, 859 struct proc_dir_entry *parent_dir, 860 char *dir_name, 861 struct file_operations *info_fops, 862 struct file_operations *state_fops, 863 struct file_operations *alarm_fops, void *data) 864 { 865 struct proc_dir_entry *entry = NULL; 866 867 if (!*dir) { 868 *dir = proc_mkdir(dir_name, parent_dir); 869 if (!*dir) { 870 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 871 "proc_mkdir() failed")); 872 return -ENODEV; 873 } 874 (*dir)->owner = THIS_MODULE; 875 } 876 877 /* 'info' [R] */ 878 if (info_fops) { 879 entry = create_proc_entry(ACPI_SBS_FILE_INFO, S_IRUGO, *dir); 880 if (!entry) { 881 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 882 "create_proc_entry() failed")); 883 } else { 884 entry->proc_fops = info_fops; 885 entry->data = data; 886 entry->owner = THIS_MODULE; 887 } 888 } 889 890 /* 'state' [R] */ 891 if (state_fops) { 892 entry = create_proc_entry(ACPI_SBS_FILE_STATE, S_IRUGO, *dir); 893 if (!entry) { 894 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 895 "create_proc_entry() failed")); 896 } else { 897 entry->proc_fops = state_fops; 898 entry->data = data; 899 entry->owner = THIS_MODULE; 900 } 901 } 902 903 /* 'alarm' [R/W] */ 904 if (alarm_fops) { 905 entry = create_proc_entry(ACPI_SBS_FILE_ALARM, S_IRUGO, *dir); 906 if (!entry) { 907 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 908 "create_proc_entry() failed")); 909 } else { 910 entry->proc_fops = alarm_fops; 911 entry->data = data; 912 entry->owner = THIS_MODULE; 913 } 914 } 915 916 return 0; 917 } 918 919 static void 920 acpi_sbs_generic_remove_fs(struct proc_dir_entry **dir, 921 struct proc_dir_entry *parent_dir) 922 { 923 924 if (*dir) { 925 remove_proc_entry(ACPI_SBS_FILE_INFO, *dir); 926 remove_proc_entry(ACPI_SBS_FILE_STATE, *dir); 927 remove_proc_entry(ACPI_SBS_FILE_ALARM, *dir); 928 remove_proc_entry((*dir)->name, parent_dir); 929 *dir = NULL; 930 } 931 932 } 933 934 /* Smart Battery Interface */ 935 936 static struct proc_dir_entry *acpi_battery_dir = NULL; 937 938 static int acpi_battery_read_info(struct seq_file *seq, void *offset) 939 { 940 struct acpi_battery *battery = seq->private; 941 struct acpi_sbs *sbs = battery->sbs; 942 int cscale; 943 int result = 0; 944 945 if (sbs_mutex_lock(sbs)) { 946 return -ENODEV; 947 } 948 949 result = acpi_check_update_proc(sbs); 950 if (result) 951 goto end; 952 953 if (update_time == 0) { 954 result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_INFO); 955 if (result) { 956 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 957 "acpi_sbs_update_run() failed")); 958 } 959 } 960 961 if (battery->battery_present) { 962 seq_printf(seq, "present: yes\n"); 963 } else { 964 seq_printf(seq, "present: no\n"); 965 goto end; 966 } 967 968 if (battery->info.capacity_mode) { 969 cscale = battery->info.vscale * battery->info.ipscale; 970 } else { 971 cscale = battery->info.ipscale; 972 } 973 seq_printf(seq, "design capacity: %i%s\n", 974 battery->info.design_capacity * cscale, 975 battery->info.capacity_mode ? "0 mWh" : " mAh"); 976 977 seq_printf(seq, "last full capacity: %i%s\n", 978 battery->info.full_charge_capacity * cscale, 979 battery->info.capacity_mode ? "0 mWh" : " mAh"); 980 981 seq_printf(seq, "battery technology: rechargeable\n"); 982 983 seq_printf(seq, "design voltage: %i mV\n", 984 battery->info.design_voltage * battery->info.vscale); 985 986 seq_printf(seq, "design capacity warning: unknown\n"); 987 seq_printf(seq, "design capacity low: unknown\n"); 988 seq_printf(seq, "capacity granularity 1: unknown\n"); 989 seq_printf(seq, "capacity granularity 2: unknown\n"); 990 991 seq_printf(seq, "model number: %s\n", 992 battery->info.device_name); 993 994 seq_printf(seq, "serial number: %i\n", 995 battery->info.serial_number); 996 997 seq_printf(seq, "battery type: %s\n", 998 battery->info.device_chemistry); 999 1000 seq_printf(seq, "OEM info: %s\n", 1001 battery->info.manufacturer_name); 1002 1003 end: 1004 1005 sbs_mutex_unlock(sbs); 1006 1007 return result; 1008 } 1009 1010 static int acpi_battery_info_open_fs(struct inode *inode, struct file *file) 1011 { 1012 return single_open(file, acpi_battery_read_info, PDE(inode)->data); 1013 } 1014 1015 static int acpi_battery_read_state(struct seq_file *seq, void *offset) 1016 { 1017 struct acpi_battery *battery = seq->private; 1018 struct acpi_sbs *sbs = battery->sbs; 1019 int result = 0; 1020 int cscale; 1021 int foo; 1022 1023 if (sbs_mutex_lock(sbs)) { 1024 return -ENODEV; 1025 } 1026 1027 result = acpi_check_update_proc(sbs); 1028 if (result) 1029 goto end; 1030 1031 if (update_time == 0) { 1032 result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_STATE); 1033 if (result) { 1034 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1035 "acpi_sbs_update_run() failed")); 1036 } 1037 } 1038 1039 if (battery->battery_present) { 1040 seq_printf(seq, "present: yes\n"); 1041 } else { 1042 seq_printf(seq, "present: no\n"); 1043 goto end; 1044 } 1045 1046 if (battery->info.capacity_mode) { 1047 cscale = battery->info.vscale * battery->info.ipscale; 1048 } else { 1049 cscale = battery->info.ipscale; 1050 } 1051 1052 if (battery->state.battery_state & 0x0010) { 1053 seq_printf(seq, "capacity state: critical\n"); 1054 } else { 1055 seq_printf(seq, "capacity state: ok\n"); 1056 } 1057 1058 foo = (s16) battery->state.amperage * battery->info.ipscale; 1059 if (battery->info.capacity_mode) { 1060 foo = foo * battery->info.design_voltage / 1000; 1061 } 1062 if (battery->state.amperage < 0) { 1063 seq_printf(seq, "charging state: discharging\n"); 1064 seq_printf(seq, "present rate: %d %s\n", 1065 -foo, battery->info.capacity_mode ? "mW" : "mA"); 1066 } else if (battery->state.amperage > 0) { 1067 seq_printf(seq, "charging state: charging\n"); 1068 seq_printf(seq, "present rate: %d %s\n", 1069 foo, battery->info.capacity_mode ? "mW" : "mA"); 1070 } else { 1071 seq_printf(seq, "charging state: charged\n"); 1072 seq_printf(seq, "present rate: 0 %s\n", 1073 battery->info.capacity_mode ? "mW" : "mA"); 1074 } 1075 1076 seq_printf(seq, "remaining capacity: %i%s\n", 1077 battery->state.remaining_capacity * cscale, 1078 battery->info.capacity_mode ? "0 mWh" : " mAh"); 1079 1080 seq_printf(seq, "present voltage: %i mV\n", 1081 battery->state.voltage * battery->info.vscale); 1082 1083 end: 1084 1085 sbs_mutex_unlock(sbs); 1086 1087 return result; 1088 } 1089 1090 static int acpi_battery_state_open_fs(struct inode *inode, struct file *file) 1091 { 1092 return single_open(file, acpi_battery_read_state, PDE(inode)->data); 1093 } 1094 1095 static int acpi_battery_read_alarm(struct seq_file *seq, void *offset) 1096 { 1097 struct acpi_battery *battery = seq->private; 1098 struct acpi_sbs *sbs = battery->sbs; 1099 int result = 0; 1100 int cscale; 1101 1102 if (sbs_mutex_lock(sbs)) { 1103 return -ENODEV; 1104 } 1105 1106 result = acpi_check_update_proc(sbs); 1107 if (result) 1108 goto end; 1109 1110 if (update_time == 0) { 1111 result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_ALARM); 1112 if (result) { 1113 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1114 "acpi_sbs_update_run() failed")); 1115 } 1116 } 1117 1118 if (!battery->battery_present) { 1119 seq_printf(seq, "present: no\n"); 1120 goto end; 1121 } 1122 1123 if (battery->info.capacity_mode) { 1124 cscale = battery->info.vscale * battery->info.ipscale; 1125 } else { 1126 cscale = battery->info.ipscale; 1127 } 1128 1129 seq_printf(seq, "alarm: "); 1130 if (battery->alarm.remaining_capacity) { 1131 seq_printf(seq, "%i%s\n", 1132 battery->alarm.remaining_capacity * cscale, 1133 battery->info.capacity_mode ? "0 mWh" : " mAh"); 1134 } else { 1135 seq_printf(seq, "disabled\n"); 1136 } 1137 1138 end: 1139 1140 sbs_mutex_unlock(sbs); 1141 1142 return result; 1143 } 1144 1145 static ssize_t 1146 acpi_battery_write_alarm(struct file *file, const char __user * buffer, 1147 size_t count, loff_t * ppos) 1148 { 1149 struct seq_file *seq = file->private_data; 1150 struct acpi_battery *battery = seq->private; 1151 struct acpi_sbs *sbs = battery->sbs; 1152 char alarm_string[12] = { '\0' }; 1153 int result, old_alarm, new_alarm; 1154 1155 if (sbs_mutex_lock(sbs)) { 1156 return -ENODEV; 1157 } 1158 1159 result = acpi_check_update_proc(sbs); 1160 if (result) 1161 goto end; 1162 1163 if (!battery->battery_present) { 1164 result = -ENODEV; 1165 goto end; 1166 } 1167 1168 if (count > sizeof(alarm_string) - 1) { 1169 result = -EINVAL; 1170 goto end; 1171 } 1172 1173 if (copy_from_user(alarm_string, buffer, count)) { 1174 result = -EFAULT; 1175 goto end; 1176 } 1177 1178 alarm_string[count] = 0; 1179 1180 old_alarm = battery->alarm.remaining_capacity; 1181 new_alarm = simple_strtoul(alarm_string, NULL, 0); 1182 1183 result = acpi_battery_set_alarm(battery, new_alarm); 1184 if (result) { 1185 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1186 "acpi_battery_set_alarm() failed")); 1187 acpi_battery_set_alarm(battery, old_alarm); 1188 goto end; 1189 } 1190 result = acpi_battery_get_alarm(battery); 1191 if (result) { 1192 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1193 "acpi_battery_get_alarm() failed")); 1194 acpi_battery_set_alarm(battery, old_alarm); 1195 goto end; 1196 } 1197 1198 end: 1199 sbs_mutex_unlock(sbs); 1200 1201 if (result) { 1202 return result; 1203 } else { 1204 return count; 1205 } 1206 } 1207 1208 static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file) 1209 { 1210 return single_open(file, acpi_battery_read_alarm, PDE(inode)->data); 1211 } 1212 1213 static struct file_operations acpi_battery_info_fops = { 1214 .open = acpi_battery_info_open_fs, 1215 .read = seq_read, 1216 .llseek = seq_lseek, 1217 .release = single_release, 1218 .owner = THIS_MODULE, 1219 }; 1220 1221 static struct file_operations acpi_battery_state_fops = { 1222 .open = acpi_battery_state_open_fs, 1223 .read = seq_read, 1224 .llseek = seq_lseek, 1225 .release = single_release, 1226 .owner = THIS_MODULE, 1227 }; 1228 1229 static struct file_operations acpi_battery_alarm_fops = { 1230 .open = acpi_battery_alarm_open_fs, 1231 .read = seq_read, 1232 .write = acpi_battery_write_alarm, 1233 .llseek = seq_lseek, 1234 .release = single_release, 1235 .owner = THIS_MODULE, 1236 }; 1237 1238 /* Legacy AC Adapter Interface */ 1239 1240 static struct proc_dir_entry *acpi_ac_dir = NULL; 1241 1242 static int acpi_ac_read_state(struct seq_file *seq, void *offset) 1243 { 1244 struct acpi_sbs *sbs = seq->private; 1245 int result; 1246 1247 if (sbs_mutex_lock(sbs)) { 1248 return -ENODEV; 1249 } 1250 1251 if (update_time == 0) { 1252 result = acpi_sbs_update_run(sbs, -1, DATA_TYPE_AC_STATE); 1253 if (result) { 1254 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1255 "acpi_sbs_update_run() failed")); 1256 } 1257 } 1258 1259 seq_printf(seq, "state: %s\n", 1260 sbs->ac.ac_present ? "on-line" : "off-line"); 1261 1262 sbs_mutex_unlock(sbs); 1263 1264 return 0; 1265 } 1266 1267 static int acpi_ac_state_open_fs(struct inode *inode, struct file *file) 1268 { 1269 return single_open(file, acpi_ac_read_state, PDE(inode)->data); 1270 } 1271 1272 static struct file_operations acpi_ac_state_fops = { 1273 .open = acpi_ac_state_open_fs, 1274 .read = seq_read, 1275 .llseek = seq_lseek, 1276 .release = single_release, 1277 .owner = THIS_MODULE, 1278 }; 1279 1280 /* -------------------------------------------------------------------------- 1281 Driver Interface 1282 -------------------------------------------------------------------------- */ 1283 1284 /* Smart Battery */ 1285 1286 static int acpi_battery_add(struct acpi_sbs *sbs, int id) 1287 { 1288 int is_present; 1289 int result; 1290 char dir_name[32]; 1291 struct acpi_battery *battery; 1292 1293 battery = &sbs->battery[id]; 1294 1295 battery->alive = 0; 1296 1297 battery->init_state = 0; 1298 battery->id = id; 1299 battery->sbs = sbs; 1300 1301 result = acpi_battery_select(battery); 1302 if (result) { 1303 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1304 "acpi_battery_select() failed")); 1305 goto end; 1306 } 1307 1308 result = acpi_battery_get_present(battery); 1309 if (result) { 1310 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1311 "acpi_battery_get_present() failed")); 1312 goto end; 1313 } 1314 1315 is_present = battery->battery_present; 1316 1317 if (is_present) { 1318 result = acpi_battery_init(battery); 1319 if (result) { 1320 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1321 "acpi_battery_init() failed")); 1322 goto end; 1323 } 1324 battery->init_state = 1; 1325 } 1326 1327 sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id); 1328 1329 result = acpi_sbs_generic_add_fs(&battery->battery_entry, 1330 acpi_battery_dir, 1331 dir_name, 1332 &acpi_battery_info_fops, 1333 &acpi_battery_state_fops, 1334 &acpi_battery_alarm_fops, battery); 1335 if (result) { 1336 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1337 "acpi_sbs_generic_add_fs() failed")); 1338 goto end; 1339 } 1340 battery->alive = 1; 1341 1342 printk(KERN_INFO PREFIX "%s [%s]: Battery Slot [%s] (battery %s)\n", 1343 ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device), dir_name, 1344 sbs->battery->battery_present ? "present" : "absent"); 1345 1346 end: 1347 return result; 1348 } 1349 1350 static void acpi_battery_remove(struct acpi_sbs *sbs, int id) 1351 { 1352 1353 if (sbs->battery[id].battery_entry) { 1354 acpi_sbs_generic_remove_fs(&(sbs->battery[id].battery_entry), 1355 acpi_battery_dir); 1356 } 1357 } 1358 1359 static int acpi_ac_add(struct acpi_sbs *sbs) 1360 { 1361 int result; 1362 1363 result = acpi_ac_get_present(sbs); 1364 if (result) { 1365 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1366 "acpi_ac_get_present() failed")); 1367 goto end; 1368 } 1369 1370 result = acpi_sbs_generic_add_fs(&sbs->ac_entry, 1371 acpi_ac_dir, 1372 ACPI_AC_DIR_NAME, 1373 NULL, &acpi_ac_state_fops, NULL, sbs); 1374 if (result) { 1375 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1376 "acpi_sbs_generic_add_fs() failed")); 1377 goto end; 1378 } 1379 1380 printk(KERN_INFO PREFIX "%s [%s]: AC Adapter [%s] (%s)\n", 1381 ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device), 1382 ACPI_AC_DIR_NAME, sbs->ac.ac_present ? "on-line" : "off-line"); 1383 1384 end: 1385 1386 return result; 1387 } 1388 1389 static void acpi_ac_remove(struct acpi_sbs *sbs) 1390 { 1391 1392 if (sbs->ac_entry) { 1393 acpi_sbs_generic_remove_fs(&sbs->ac_entry, acpi_ac_dir); 1394 } 1395 } 1396 1397 static void acpi_sbs_update_time_run(unsigned long data) 1398 { 1399 acpi_os_execute(OSL_GPE_HANDLER, acpi_sbs_update_time, (void *)data); 1400 } 1401 1402 static int acpi_sbs_update_run(struct acpi_sbs *sbs, int id, int data_type) 1403 { 1404 struct acpi_battery *battery; 1405 int result = 0, cnt; 1406 int old_ac_present = -1; 1407 int old_battery_present = -1; 1408 int new_ac_present = -1; 1409 int new_battery_present = -1; 1410 int id_min = 0, id_max = MAX_SBS_BAT - 1; 1411 char dir_name[32]; 1412 int do_battery_init = 0, do_ac_init = 0; 1413 int old_remaining_capacity = 0; 1414 int update_ac = 1, update_battery = 1; 1415 int up_tm = update_time; 1416 1417 if (sbs_zombie(sbs)) { 1418 goto end; 1419 } 1420 1421 if (id >= 0) { 1422 id_min = id_max = id; 1423 } 1424 1425 if (data_type == DATA_TYPE_COMMON && up_tm > 0) { 1426 cnt = up_tm / (up_tm > UPDATE_DELAY ? UPDATE_DELAY : up_tm); 1427 if (sbs->run_cnt % cnt != 0) { 1428 update_battery = 0; 1429 } 1430 } 1431 1432 sbs->run_cnt++; 1433 1434 if (!update_ac && !update_battery) { 1435 goto end; 1436 } 1437 1438 old_ac_present = sbs->ac.ac_present; 1439 1440 result = acpi_ac_get_present(sbs); 1441 if (result) { 1442 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1443 "acpi_ac_get_present() failed")); 1444 } 1445 1446 new_ac_present = sbs->ac.ac_present; 1447 1448 do_ac_init = (old_ac_present != new_ac_present); 1449 if (sbs->run_cnt == 1 && data_type == DATA_TYPE_COMMON) { 1450 do_ac_init = 1; 1451 } 1452 1453 if (do_ac_init) { 1454 result = acpi_sbs_generate_event(sbs->device, 1455 ACPI_SBS_AC_NOTIFY_STATUS, 1456 new_ac_present, 1457 ACPI_AC_DIR_NAME, 1458 ACPI_AC_CLASS); 1459 if (result) { 1460 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1461 "acpi_sbs_generate_event() failed")); 1462 } 1463 } 1464 1465 if (data_type == DATA_TYPE_COMMON) { 1466 if (!do_ac_init && !update_battery) { 1467 goto end; 1468 } 1469 } 1470 1471 if (data_type == DATA_TYPE_AC_STATE && !do_ac_init) { 1472 goto end; 1473 } 1474 1475 for (id = id_min; id <= id_max; id++) { 1476 battery = &sbs->battery[id]; 1477 if (battery->alive == 0) { 1478 continue; 1479 } 1480 1481 old_remaining_capacity = battery->state.remaining_capacity; 1482 1483 old_battery_present = battery->battery_present; 1484 1485 result = acpi_battery_select(battery); 1486 if (result) { 1487 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1488 "acpi_battery_select() failed")); 1489 } 1490 1491 result = acpi_battery_get_present(battery); 1492 if (result) { 1493 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1494 "acpi_battery_get_present() failed")); 1495 } 1496 1497 new_battery_present = battery->battery_present; 1498 1499 do_battery_init = ((old_battery_present != new_battery_present) 1500 && new_battery_present); 1501 if (!new_battery_present) 1502 goto event; 1503 if (do_ac_init || do_battery_init) { 1504 result = acpi_battery_init(battery); 1505 if (result) { 1506 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1507 "acpi_battery_init() " 1508 "failed")); 1509 } 1510 } 1511 if (sbs_zombie(sbs)) { 1512 goto end; 1513 } 1514 1515 if ((data_type == DATA_TYPE_COMMON 1516 || data_type == DATA_TYPE_INFO) 1517 && new_battery_present) { 1518 result = acpi_battery_get_info(battery); 1519 if (result) { 1520 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1521 "acpi_battery_get_info() failed")); 1522 } 1523 } 1524 if (data_type == DATA_TYPE_INFO) { 1525 continue; 1526 } 1527 if (sbs_zombie(sbs)) { 1528 goto end; 1529 } 1530 1531 if ((data_type == DATA_TYPE_COMMON 1532 || data_type == DATA_TYPE_STATE) 1533 && new_battery_present) { 1534 result = acpi_battery_get_state(battery); 1535 if (result) { 1536 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1537 "acpi_battery_get_state() failed")); 1538 } 1539 } 1540 if (data_type == DATA_TYPE_STATE) { 1541 goto event; 1542 } 1543 if (sbs_zombie(sbs)) { 1544 goto end; 1545 } 1546 1547 if ((data_type == DATA_TYPE_COMMON 1548 || data_type == DATA_TYPE_ALARM) 1549 && new_battery_present) { 1550 result = acpi_battery_get_alarm(battery); 1551 if (result) { 1552 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1553 "acpi_battery_get_alarm() " 1554 "failed")); 1555 } 1556 } 1557 if (data_type == DATA_TYPE_ALARM) { 1558 continue; 1559 } 1560 if (sbs_zombie(sbs)) { 1561 goto end; 1562 } 1563 1564 event: 1565 1566 if (old_battery_present != new_battery_present || do_ac_init || 1567 old_remaining_capacity != 1568 battery->state.remaining_capacity) { 1569 sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id); 1570 result = acpi_sbs_generate_event(sbs->device, 1571 ACPI_SBS_BATTERY_NOTIFY_STATUS, 1572 new_battery_present, 1573 dir_name, 1574 ACPI_BATTERY_CLASS); 1575 if (result) { 1576 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1577 "acpi_sbs_generate_event() " 1578 "failed")); 1579 } 1580 } 1581 } 1582 1583 end: 1584 1585 return result; 1586 } 1587 1588 static void acpi_sbs_update_time(void *data) 1589 { 1590 struct acpi_sbs *sbs = data; 1591 unsigned long delay = -1; 1592 int result; 1593 unsigned int up_tm = update_time; 1594 1595 if (sbs_mutex_lock(sbs)) 1596 return; 1597 1598 result = acpi_sbs_update_run(sbs, -1, DATA_TYPE_COMMON); 1599 if (result) { 1600 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1601 "acpi_sbs_update_run() failed")); 1602 } 1603 1604 if (sbs_zombie(sbs)) { 1605 goto end; 1606 } 1607 1608 if (!up_tm) { 1609 if (timer_pending(&sbs->update_timer)) 1610 del_timer(&sbs->update_timer); 1611 } else { 1612 delay = (up_tm > UPDATE_DELAY ? UPDATE_DELAY : up_tm); 1613 delay = jiffies + HZ * delay; 1614 if (timer_pending(&sbs->update_timer)) { 1615 mod_timer(&sbs->update_timer, delay); 1616 } else { 1617 sbs->update_timer.data = (unsigned long)data; 1618 sbs->update_timer.function = acpi_sbs_update_time_run; 1619 sbs->update_timer.expires = delay; 1620 add_timer(&sbs->update_timer); 1621 } 1622 } 1623 1624 end: 1625 1626 sbs_mutex_unlock(sbs); 1627 } 1628 1629 static int acpi_sbs_add(struct acpi_device *device) 1630 { 1631 struct acpi_sbs *sbs = NULL; 1632 int result = 0, remove_result = 0; 1633 unsigned long sbs_obj; 1634 int id; 1635 acpi_status status = AE_OK; 1636 unsigned long val; 1637 1638 status = 1639 acpi_evaluate_integer(device->parent->handle, "_EC", NULL, &val); 1640 if (ACPI_FAILURE(status)) { 1641 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Error obtaining _EC")); 1642 return -EIO; 1643 } 1644 1645 sbs = kzalloc(sizeof(struct acpi_sbs), GFP_KERNEL); 1646 if (!sbs) { 1647 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "kzalloc() failed")); 1648 result = -ENOMEM; 1649 goto end; 1650 } 1651 1652 mutex_init(&sbs->mutex); 1653 1654 sbs_mutex_lock(sbs); 1655 1656 sbs->base = (val & 0xff00ull) >> 8; 1657 sbs->device = device; 1658 1659 strcpy(acpi_device_name(device), ACPI_SBS_DEVICE_NAME); 1660 strcpy(acpi_device_class(device), ACPI_SBS_CLASS); 1661 acpi_driver_data(device) = sbs; 1662 1663 result = acpi_ac_add(sbs); 1664 if (result) { 1665 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "acpi_ac_add() failed")); 1666 goto end; 1667 } 1668 status = acpi_evaluate_integer(device->handle, "_SBS", NULL, &sbs_obj); 1669 if (status) { 1670 ACPI_EXCEPTION((AE_INFO, status, 1671 "acpi_evaluate_integer() failed")); 1672 result = -EIO; 1673 goto end; 1674 } 1675 if (sbs_obj > 0) { 1676 result = acpi_sbsm_get_info(sbs); 1677 if (result) { 1678 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1679 "acpi_sbsm_get_info() failed")); 1680 goto end; 1681 } 1682 sbs->sbsm_present = 1; 1683 } 1684 1685 if (sbs->sbsm_present == 0) { 1686 result = acpi_battery_add(sbs, 0); 1687 if (result) { 1688 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1689 "acpi_battery_add() failed")); 1690 goto end; 1691 } 1692 } else { 1693 for (id = 0; id < MAX_SBS_BAT; id++) { 1694 if ((sbs->sbsm_batteries_supported & (1 << id))) { 1695 result = acpi_battery_add(sbs, id); 1696 if (result) { 1697 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1698 "acpi_battery_add() failed")); 1699 goto end; 1700 } 1701 } 1702 } 1703 } 1704 1705 sbs->handle = device->handle; 1706 1707 init_timer(&sbs->update_timer); 1708 result = acpi_check_update_proc(sbs); 1709 if (result) 1710 goto end; 1711 1712 end: 1713 1714 sbs_mutex_unlock(sbs); 1715 1716 if (result) { 1717 remove_result = acpi_sbs_remove(device, 0); 1718 if (remove_result) { 1719 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1720 "acpi_sbs_remove() failed")); 1721 } 1722 } 1723 1724 return result; 1725 } 1726 1727 static int acpi_sbs_remove(struct acpi_device *device, int type) 1728 { 1729 struct acpi_sbs *sbs; 1730 int id; 1731 1732 if (!device) { 1733 return -EINVAL; 1734 } 1735 1736 sbs = acpi_driver_data(device); 1737 if (!sbs) { 1738 return -EINVAL; 1739 } 1740 1741 sbs_mutex_lock(sbs); 1742 1743 sbs->zombie = 1; 1744 del_timer_sync(&sbs->update_timer); 1745 acpi_os_wait_events_complete(NULL); 1746 del_timer_sync(&sbs->update_timer); 1747 1748 for (id = 0; id < MAX_SBS_BAT; id++) { 1749 acpi_battery_remove(sbs, id); 1750 } 1751 1752 acpi_ac_remove(sbs); 1753 1754 sbs_mutex_unlock(sbs); 1755 1756 mutex_destroy(&sbs->mutex); 1757 1758 kfree(sbs); 1759 1760 return 0; 1761 } 1762 1763 static void acpi_sbs_rmdirs(void) 1764 { 1765 if (acpi_ac_dir) { 1766 acpi_unlock_ac_dir(acpi_ac_dir); 1767 acpi_ac_dir = NULL; 1768 } 1769 if (acpi_battery_dir) { 1770 acpi_unlock_battery_dir(acpi_battery_dir); 1771 acpi_battery_dir = NULL; 1772 } 1773 } 1774 1775 static int acpi_sbs_resume(struct acpi_device *device) 1776 { 1777 struct acpi_sbs *sbs; 1778 1779 if (!device) 1780 return -EINVAL; 1781 1782 sbs = device->driver_data; 1783 1784 sbs->run_cnt = 0; 1785 1786 return 0; 1787 } 1788 1789 static int __init acpi_sbs_init(void) 1790 { 1791 int result = 0; 1792 1793 if (acpi_disabled) 1794 return -ENODEV; 1795 1796 if (capacity_mode != DEF_CAPACITY_UNIT 1797 && capacity_mode != MAH_CAPACITY_UNIT 1798 && capacity_mode != MWH_CAPACITY_UNIT) { 1799 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1800 "invalid capacity_mode = %d", capacity_mode)); 1801 return -EINVAL; 1802 } 1803 1804 acpi_ac_dir = acpi_lock_ac_dir(); 1805 if (!acpi_ac_dir) { 1806 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1807 "acpi_lock_ac_dir() failed")); 1808 return -ENODEV; 1809 } 1810 1811 acpi_battery_dir = acpi_lock_battery_dir(); 1812 if (!acpi_battery_dir) { 1813 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1814 "acpi_lock_battery_dir() failed")); 1815 acpi_sbs_rmdirs(); 1816 return -ENODEV; 1817 } 1818 1819 result = acpi_bus_register_driver(&acpi_sbs_driver); 1820 if (result < 0) { 1821 ACPI_EXCEPTION((AE_INFO, AE_ERROR, 1822 "acpi_bus_register_driver() failed")); 1823 acpi_sbs_rmdirs(); 1824 return -ENODEV; 1825 } 1826 1827 return 0; 1828 } 1829 1830 static void __exit acpi_sbs_exit(void) 1831 { 1832 acpi_bus_unregister_driver(&acpi_sbs_driver); 1833 1834 acpi_sbs_rmdirs(); 1835 1836 return; 1837 } 1838 1839 module_init(acpi_sbs_init); 1840 module_exit(acpi_sbs_exit); 1841