sbs.c 41.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 *  acpi_sbs.c - ACPI Smart Battery System Driver ($Revision: 1.16 $)
 *
 *  Copyright (c) 2005 Rich Townsend <rhdt@bartol.udel.edu>
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or (at
 *  your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <asm/uaccess.h>
#include <linux/acpi.h>
33
#include <linux/timer.h>
34
#include <linux/jiffies.h>
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#include <linux/delay.h>

#define ACPI_SBS_COMPONENT		0x00080000
#define ACPI_SBS_CLASS			"sbs"
#define ACPI_AC_CLASS			"ac_adapter"
#define ACPI_BATTERY_CLASS		"battery"
#define ACPI_SBS_HID			"ACPI0002"
#define ACPI_SBS_DEVICE_NAME		"Smart Battery System"
#define ACPI_SBS_FILE_INFO		"info"
#define ACPI_SBS_FILE_STATE		"state"
#define ACPI_SBS_FILE_ALARM		"alarm"
#define ACPI_BATTERY_DIR_NAME		"BAT%i"
#define ACPI_AC_DIR_NAME		"AC0"
#define ACPI_SBC_SMBUS_ADDR		0x9
#define ACPI_SBSM_SMBUS_ADDR		0xa
#define ACPI_SB_SMBUS_ADDR		0xb
#define ACPI_SBS_AC_NOTIFY_STATUS	0x80
#define ACPI_SBS_BATTERY_NOTIFY_STATUS	0x80
#define ACPI_SBS_BATTERY_NOTIFY_INFO	0x81

#define _COMPONENT			ACPI_SBS_COMPONENT

57
ACPI_MODULE_NAME("sbs");
58 59 60 61 62

MODULE_AUTHOR("Rich Townsend");
MODULE_DESCRIPTION("Smart Battery System ACPI interface driver");
MODULE_LICENSE("GPL");

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
#define	xmsleep(t)	msleep(t)

#define ACPI_EC_SMB_PRTCL	0x00	/* protocol, PEC */

#define ACPI_EC_SMB_STS		0x01	/* status */
#define ACPI_EC_SMB_ADDR	0x02	/* address */
#define ACPI_EC_SMB_CMD		0x03	/* command */
#define ACPI_EC_SMB_DATA	0x04	/* 32 data registers */
#define ACPI_EC_SMB_BCNT	0x24	/* number of data bytes */

#define ACPI_EC_SMB_STS_DONE	0x80
#define ACPI_EC_SMB_STS_STATUS	0x1f

#define ACPI_EC_SMB_PRTCL_WRITE		0x00
#define ACPI_EC_SMB_PRTCL_READ		0x01
#define ACPI_EC_SMB_PRTCL_WORD_DATA	0x08
#define ACPI_EC_SMB_PRTCL_BLOCK_DATA	0x0a

#define ACPI_EC_SMB_TRANSACTION_SLEEP	1
#define ACPI_EC_SMB_ACCESS_SLEEP1	1
#define ACPI_EC_SMB_ACCESS_SLEEP2	10

#define	DEF_CAPACITY_UNIT	3
#define	MAH_CAPACITY_UNIT	1
#define	MWH_CAPACITY_UNIT	2
#define	CAPACITY_UNIT		DEF_CAPACITY_UNIT

#define	REQUEST_UPDATE_MODE	1
#define	QUEUE_UPDATE_MODE	2

#define	DATA_TYPE_COMMON	0
#define	DATA_TYPE_INFO		1
#define	DATA_TYPE_STATE		2
#define	DATA_TYPE_ALARM		3
#define	DATA_TYPE_AC_STATE	4

extern struct proc_dir_entry *acpi_lock_ac_dir(void);
extern struct proc_dir_entry *acpi_lock_battery_dir(void);
extern void acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir);
extern void acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);

#define	MAX_SBS_BAT			4
#define ACPI_SBS_BLOCK_MAX		32

#define ACPI_SBS_SMBUS_READ		1
#define ACPI_SBS_SMBUS_WRITE		2

#define ACPI_SBS_WORD_DATA		1
#define ACPI_SBS_BLOCK_DATA		2

113
#define	UPDATE_DELAY	10
114

115 116
/* 0 - every time, > 0 - by update_time */
static unsigned int update_time = 120;
117

118
static unsigned int capacity_mode = CAPACITY_UNIT;
119

120 121
module_param(update_time, uint, 0644);
module_param(capacity_mode, uint, 0444);
122 123 124

static int acpi_sbs_add(struct acpi_device *device);
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 = {
L
Len Brown 已提交
128
	.name = "sbs",
129 130 131 132 133
	.class = ACPI_SBS_CLASS,
	.ids = ACPI_SBS_HID,
	.ops = {
		.add = acpi_sbs_add,
		.remove = acpi_sbs_remove,
134
		.resume = acpi_sbs_resume,
135 136 137
		},
};

138 139 140 141
struct acpi_ac {
	int ac_present;
};

142 143 144 145 146 147 148 149
struct acpi_battery_info {
	int capacity_mode;
	s16 full_charge_capacity;
	s16 design_capacity;
	s16 design_voltage;
	int vscale;
	int ipscale;
	s16 serial_number;
150 151 152
	char manufacturer_name[ACPI_SBS_BLOCK_MAX + 3];
	char device_name[ACPI_SBS_BLOCK_MAX + 3];
	char device_chemistry[ACPI_SBS_BLOCK_MAX + 3];
153 154 155 156 157 158
};

struct acpi_battery_state {
	s16 voltage;
	s16 amperage;
	s16 remaining_capacity;
159
	s16 battery_state;
160 161 162 163 164 165 166 167 168 169
};

struct acpi_battery_alarm {
	s16 remaining_capacity;
};

struct acpi_battery {
	int alive;
	int id;
	int init_state;
170
	int battery_present;
171 172 173 174 175 176 177 178 179
	struct acpi_sbs *sbs;
	struct acpi_battery_info info;
	struct acpi_battery_state state;
	struct acpi_battery_alarm alarm;
	struct proc_dir_entry *battery_entry;
};

struct acpi_sbs {
	acpi_handle handle;
180
	int base;
181
	struct acpi_device *device;
182 183
	struct acpi_ec_smbus *smbus;
	struct mutex mutex;
184 185 186
	int sbsm_present;
	int sbsm_batteries_supported;
	struct proc_dir_entry *ac_entry;
187
	struct acpi_ac ac;
188 189 190
	struct acpi_battery battery[MAX_SBS_BAT];
	int zombie;
	struct timer_list update_timer;
191 192
	int run_cnt;
	int update_proc_flg;
193 194
};

195 196 197
static int acpi_sbs_update_run(struct acpi_sbs *sbs, int id, int data_type);
static void acpi_sbs_update_time(void *data);

198 199 200 201 202 203 204 205
union sbs_rw_data {
	u16 word;
	u8 block[ACPI_SBS_BLOCK_MAX + 2];
};

static int acpi_ec_sbs_access(struct acpi_sbs *sbs, u16 addr,
			      char read_write, u8 command, int size,
			      union sbs_rw_data *data);
206 207 208 209 210

/* --------------------------------------------------------------------------
                               SMBus Communication
   -------------------------------------------------------------------------- */

211
static int acpi_ec_sbs_read(struct acpi_sbs *sbs, u8 address, u8 * data)
212
{
213 214
	u8 val;
	int err;
215

216 217 218 219 220 221 222
	err = ec_read(sbs->base + address, &val);
	if (!err) {
		*data = val;
	}
	xmsleep(ACPI_EC_SMB_TRANSACTION_SLEEP);
	return (err);
}
223

224 225 226
static int acpi_ec_sbs_write(struct acpi_sbs *sbs, u8 address, u8 data)
{
	int err;
227

228 229 230
	err = ec_write(sbs->base + address, data);
	return (err);
}
231

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
static int
acpi_ec_sbs_access(struct acpi_sbs *sbs, u16 addr,
		   char read_write, u8 command, int size,
		   union sbs_rw_data *data)
{
	unsigned char protocol, len = 0, temp[2] = { 0, 0 };
	int i;

	if (read_write == ACPI_SBS_SMBUS_READ) {
		protocol = ACPI_EC_SMB_PRTCL_READ;
	} else {
		protocol = ACPI_EC_SMB_PRTCL_WRITE;
	}

	switch (size) {

	case ACPI_SBS_WORD_DATA:
		acpi_ec_sbs_write(sbs, ACPI_EC_SMB_CMD, command);
		if (read_write == ACPI_SBS_SMBUS_WRITE) {
			acpi_ec_sbs_write(sbs, ACPI_EC_SMB_DATA, data->word);
			acpi_ec_sbs_write(sbs, ACPI_EC_SMB_DATA + 1,
					  data->word >> 8);
		}
		protocol |= ACPI_EC_SMB_PRTCL_WORD_DATA;
256
		break;
257 258 259 260 261 262 263 264 265 266
	case ACPI_SBS_BLOCK_DATA:
		acpi_ec_sbs_write(sbs, ACPI_EC_SMB_CMD, command);
		if (read_write == ACPI_SBS_SMBUS_WRITE) {
			len = min_t(u8, data->block[0], 32);
			acpi_ec_sbs_write(sbs, ACPI_EC_SMB_BCNT, len);
			for (i = 0; i < len; i++)
				acpi_ec_sbs_write(sbs, ACPI_EC_SMB_DATA + i,
						  data->block[i + 1]);
		}
		protocol |= ACPI_EC_SMB_PRTCL_BLOCK_DATA;
267
		break;
268 269
	default:
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
270
				"unsupported transaction %d", size));
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
		return (-1);
	}

	acpi_ec_sbs_write(sbs, ACPI_EC_SMB_ADDR, addr << 1);
	acpi_ec_sbs_write(sbs, ACPI_EC_SMB_PRTCL, protocol);

	acpi_ec_sbs_read(sbs, ACPI_EC_SMB_STS, temp);

	if (~temp[0] & ACPI_EC_SMB_STS_DONE) {
		xmsleep(ACPI_EC_SMB_ACCESS_SLEEP1);
		acpi_ec_sbs_read(sbs, ACPI_EC_SMB_STS, temp);
	}
	if (~temp[0] & ACPI_EC_SMB_STS_DONE) {
		xmsleep(ACPI_EC_SMB_ACCESS_SLEEP2);
		acpi_ec_sbs_read(sbs, ACPI_EC_SMB_STS, temp);
	}
	if ((~temp[0] & ACPI_EC_SMB_STS_DONE)
	    || (temp[0] & ACPI_EC_SMB_STS_STATUS)) {
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
290
				"transaction %d error", size));
291 292 293 294 295 296 297 298 299 300 301 302 303
		return (-1);
	}

	if (read_write == ACPI_SBS_SMBUS_WRITE) {
		return (0);
	}

	switch (size) {

	case ACPI_SBS_WORD_DATA:
		acpi_ec_sbs_read(sbs, ACPI_EC_SMB_DATA, temp);
		acpi_ec_sbs_read(sbs, ACPI_EC_SMB_DATA + 1, temp + 1);
		data->word = (temp[1] << 8) | temp[0];
304
		break;
305 306 307 308 309 310 311 312 313

	case ACPI_SBS_BLOCK_DATA:
		len = 0;
		acpi_ec_sbs_read(sbs, ACPI_EC_SMB_BCNT, &len);
		len = min_t(u8, len, 32);
		for (i = 0; i < len; i++)
			acpi_ec_sbs_read(sbs, ACPI_EC_SMB_DATA + i,
					 data->block + i + 1);
		data->block[0] = len;
314 315
		break;
	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 329
	int result = 0;

330 331 332 333
	result = acpi_ec_sbs_access(sbs, addr,
				    ACPI_SBS_SMBUS_READ, func,
				    ACPI_SBS_WORD_DATA, &data);
	if (result) {
334 335
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_ec_sbs_access() failed"));
336 337
	} else {
		*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 348
	int result = 0;

349 350 351 352
	result = acpi_ec_sbs_access(sbs, addr,
				    ACPI_SBS_SMBUS_READ, func,
				    ACPI_SBS_BLOCK_DATA, &data);
	if (result) {
353 354
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_ec_sbs_access() failed"));
355 356 357
	} else {
		strncpy(str, (const char *)data.block + 1, data.block[0]);
		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 368 369 370
	int result = 0;

	data.word = word;

371 372 373 374
	result = acpi_ec_sbs_access(sbs, addr,
				    ACPI_SBS_SMBUS_WRITE, func,
				    ACPI_SBS_WORD_DATA, &data);
	if (result) {
375 376
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_ec_sbs_access() failed"));
377 378
	}

379
	return result;
380 381
}

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
static int sbs_zombie(struct acpi_sbs *sbs)
{
	return (sbs->zombie);
}

static int sbs_mutex_lock(struct acpi_sbs *sbs)
{
	if (sbs_zombie(sbs)) {
		return -ENODEV;
	}
	mutex_lock(&sbs->mutex);
	return 0;
}

static void sbs_mutex_unlock(struct acpi_sbs *sbs)
{
	mutex_unlock(&sbs->mutex);
}

401 402 403 404
/* --------------------------------------------------------------------------
                            Smart Battery System Management
   -------------------------------------------------------------------------- */

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
static int acpi_check_update_proc(struct acpi_sbs *sbs)
{
	acpi_status status = AE_OK;

	if (update_time == 0) {
		sbs->update_proc_flg = 0;
		return 0;
	}
	if (sbs->update_proc_flg == 0) {
		status = acpi_os_execute(OSL_GPE_HANDLER,
					 acpi_sbs_update_time, sbs);
		if (status != AE_OK) {
			ACPI_EXCEPTION((AE_INFO, status,
					"acpi_os_execute() failed"));
			return 1;
		}
		sbs->update_proc_flg = 1;
	}
	return 0;
}
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443

static int acpi_sbs_generate_event(struct acpi_device *device,
				   int event, int state, char *bid, char *class)
{
	char bid_saved[5];
	char class_saved[20];
	int result = 0;

	strcpy(bid_saved, acpi_device_bid(device));
	strcpy(class_saved, acpi_device_class(device));

	strcpy(acpi_device_bid(device), bid);
	strcpy(acpi_device_class(device), class);

	result = acpi_bus_generate_event(device, event, state);

	strcpy(acpi_device_bid(device), bid_saved);
	strcpy(acpi_device_class(device), class_saved);

444
	return result;
445 446 447 448 449 450 451 452
}

static int acpi_battery_get_present(struct acpi_battery *battery)
{
	s16 state;
	int result = 0;
	int is_present = 0;

453 454
	result = acpi_sbs_read_word(battery->sbs,
				    ACPI_SBSM_SMBUS_ADDR, 0x01, &state);
455
	if (result) {
456 457
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_read_word() failed"));
458 459 460 461 462 463
	}
	if (!result) {
		is_present = (state & 0x000f) & (1 << battery->id);
	}
	battery->battery_present = is_present;

464
	return result;
465 466 467 468
}

static int acpi_battery_select(struct acpi_battery *battery)
{
469
	struct acpi_sbs *sbs = battery->sbs;
470 471 472 473
	int result = 0;
	s16 state;
	int foo;

474
	if (sbs->sbsm_present) {
475 476 477 478 479 480

		/* Take special care not to knobble other nibbles of
		 * state (aka selector_state), since
		 * it causes charging to halt on SBSELs */

		result =
481
		    acpi_sbs_read_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x01, &state);
482
		if (result) {
483 484
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"acpi_sbs_read_word() failed"));
485 486 487 488 489
			goto end;
		}

		foo = (state & 0x0fff) | (1 << (battery->id + 12));
		result =
490
		    acpi_sbs_write_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x01, foo);
491
		if (result) {
492 493
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"acpi_sbs_write_word() failed"));
494 495 496 497 498
			goto end;
		}
	}

      end:
499
	return result;
500 501 502 503 504 505 506
}

static int acpi_sbsm_get_info(struct acpi_sbs *sbs)
{
	int result = 0;
	s16 battery_system_info;

507 508
	result = acpi_sbs_read_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x04,
				    &battery_system_info);
509
	if (result) {
510 511
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_read_word() failed"));
512 513 514 515 516 517 518
		goto end;
	}

	sbs->sbsm_batteries_supported = battery_system_info & 0x000f;

      end:

519
	return result;
520 521 522 523
}

static int acpi_battery_get_info(struct acpi_battery *battery)
{
524
	struct acpi_sbs *sbs = battery->sbs;
525 526 527 528
	int result = 0;
	s16 battery_mode;
	s16 specification_info;

529 530
	result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x03,
				    &battery_mode);
531
	if (result) {
532 533
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_read_word() failed"));
534 535 536 537
		goto end;
	}
	battery->info.capacity_mode = (battery_mode & 0x8000) >> 15;

538 539
	result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x10,
				    &battery->info.full_charge_capacity);
540
	if (result) {
541 542
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_read_word() failed"));
543 544 545
		goto end;
	}

546 547
	result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x18,
				    &battery->info.design_capacity);
548 549

	if (result) {
550 551
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_read_word() failed"));
552 553 554
		goto end;
	}

555 556
	result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x19,
				    &battery->info.design_voltage);
557
	if (result) {
558 559
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_read_word() failed"));
560 561 562
		goto end;
	}

563 564
	result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1a,
				    &specification_info);
565
	if (result) {
566 567
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_read_word() failed"));
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
		goto end;
	}

	switch ((specification_info & 0x0f00) >> 8) {
	case 1:
		battery->info.vscale = 10;
		break;
	case 2:
		battery->info.vscale = 100;
		break;
	case 3:
		battery->info.vscale = 1000;
		break;
	default:
		battery->info.vscale = 1;
	}

	switch ((specification_info & 0xf000) >> 12) {
	case 1:
		battery->info.ipscale = 10;
		break;
	case 2:
		battery->info.ipscale = 100;
		break;
	case 3:
		battery->info.ipscale = 1000;
		break;
	default:
		battery->info.ipscale = 1;
	}

599 600
	result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1c,
				    &battery->info.serial_number);
601
	if (result) {
602 603
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_read_word() failed"));
604 605 606
		goto end;
	}

607 608
	result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x20,
				   battery->info.manufacturer_name);
609
	if (result) {
610 611
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_read_str() failed"));
612 613 614
		goto end;
	}

615 616
	result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x21,
				   battery->info.device_name);
617
	if (result) {
618 619
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_read_str() failed"));
620 621 622
		goto end;
	}

623 624
	result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x22,
				   battery->info.device_chemistry);
625
	if (result) {
626 627
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_read_str() failed"));
628 629 630 631
		goto end;
	}

      end:
632
	return result;
633 634 635 636
}

static int acpi_battery_get_state(struct acpi_battery *battery)
{
637
	struct acpi_sbs *sbs = battery->sbs;
638 639
	int result = 0;

640 641
	result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x09,
				    &battery->state.voltage);
642
	if (result) {
643 644
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_read_word() failed"));
645 646 647
		goto end;
	}

648 649
	result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x0a,
				    &battery->state.amperage);
650
	if (result) {
651 652
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_read_word() failed"));
653 654 655
		goto end;
	}

656 657
	result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x0f,
				    &battery->state.remaining_capacity);
658
	if (result) {
659 660
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_read_word() failed"));
661 662 663
		goto end;
	}

664
	result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x16,
665
				    &battery->state.battery_state);
666
	if (result) {
667 668
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_read_word() failed"));
669 670 671 672
		goto end;
	}

      end:
673
	return result;
674 675 676 677
}

static int acpi_battery_get_alarm(struct acpi_battery *battery)
{
678
	struct acpi_sbs *sbs = battery->sbs;
679 680
	int result = 0;

681 682
	result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01,
				    &battery->alarm.remaining_capacity);
683
	if (result) {
684 685
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_read_word() failed"));
686 687 688 689 690
		goto end;
	}

      end:

691
	return result;
692 693 694 695 696
}

static int acpi_battery_set_alarm(struct acpi_battery *battery,
				  unsigned long alarm)
{
697
	struct acpi_sbs *sbs = battery->sbs;
698 699 700 701 702 703
	int result = 0;
	s16 battery_mode;
	int foo;

	result = acpi_battery_select(battery);
	if (result) {
704 705
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_battery_select() failed"));
706 707 708 709 710 711 712
		goto end;
	}

	/* If necessary, enable the alarm */

	if (alarm > 0) {
		result =
713 714
		    acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x03,
				       &battery_mode);
715
		if (result) {
716 717
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"acpi_sbs_read_word() failed"));
718 719 720 721
			goto end;
		}

		result =
722 723
		    acpi_sbs_write_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01,
					battery_mode & 0xbfff);
724
		if (result) {
725 726
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"acpi_sbs_write_word() failed"));
727 728 729 730 731
			goto end;
		}
	}

	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 735
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_write_word() failed"));
736 737 738 739 740
		goto end;
	}

      end:

741
	return result;
742 743 744 745
}

static int acpi_battery_set_mode(struct acpi_battery *battery)
{
746
	struct acpi_sbs *sbs = battery->sbs;
747 748 749 750 751 752 753
	int result = 0;
	s16 battery_mode;

	if (capacity_mode == DEF_CAPACITY_UNIT) {
		goto end;
	}

754
	result = acpi_sbs_read_word(sbs,
755
				    ACPI_SB_SMBUS_ADDR, 0x03, &battery_mode);
756
	if (result) {
757 758
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_read_word() failed"));
759 760 761 762 763 764 765 766
		goto end;
	}

	if (capacity_mode == MAH_CAPACITY_UNIT) {
		battery_mode &= 0x7fff;
	} else {
		battery_mode |= 0x8000;
	}
767
	result = acpi_sbs_write_word(sbs,
768
				     ACPI_SB_SMBUS_ADDR, 0x03, battery_mode);
769
	if (result) {
770 771
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_write_word() failed"));
772 773 774
		goto end;
	}

775
	result = acpi_sbs_read_word(sbs,
776
				    ACPI_SB_SMBUS_ADDR, 0x03, &battery_mode);
777
	if (result) {
778 779
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_read_word() failed"));
780 781 782 783
		goto end;
	}

      end:
784
	return result;
785 786 787 788 789 790 791 792
}

static int acpi_battery_init(struct acpi_battery *battery)
{
	int result = 0;

	result = acpi_battery_select(battery);
	if (result) {
793
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
794
				"acpi_battery_select() failed"));
795 796 797 798 799
		goto end;
	}

	result = acpi_battery_set_mode(battery);
	if (result) {
800 801
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_battery_set_mode() failed"));
802 803 804 805 806
		goto end;
	}

	result = acpi_battery_get_info(battery);
	if (result) {
807 808
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_battery_get_info() failed"));
809 810 811 812 813
		goto end;
	}

	result = acpi_battery_get_state(battery);
	if (result) {
814 815
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_battery_get_state() failed"));
816 817 818 819 820
		goto end;
	}

	result = acpi_battery_get_alarm(battery);
	if (result) {
821 822
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_battery_get_alarm() failed"));
823 824 825 826
		goto end;
	}

      end:
827
	return result;
828 829 830 831 832 833 834
}

static int acpi_ac_get_present(struct acpi_sbs *sbs)
{
	int result = 0;
	s16 charger_status;

835 836
	result = acpi_sbs_read_word(sbs, ACPI_SBC_SMBUS_ADDR, 0x13,
				    &charger_status);
837 838

	if (result) {
839 840
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_read_word() failed"));
841 842 843
		goto end;
	}

844
	sbs->ac.ac_present = (charger_status & 0x8000) >> 15;
845 846 847

      end:

848
	return result;
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
}

/* --------------------------------------------------------------------------
                              FS Interface (/proc/acpi)
   -------------------------------------------------------------------------- */

/* Generic Routines */

static int
acpi_sbs_generic_add_fs(struct proc_dir_entry **dir,
			struct proc_dir_entry *parent_dir,
			char *dir_name,
			struct file_operations *info_fops,
			struct file_operations *state_fops,
			struct file_operations *alarm_fops, void *data)
{
	struct proc_dir_entry *entry = NULL;

	if (!*dir) {
		*dir = proc_mkdir(dir_name, parent_dir);
		if (!*dir) {
870 871
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"proc_mkdir() failed"));
872
			return -ENODEV;
873 874 875 876 877 878 879 880
		}
		(*dir)->owner = THIS_MODULE;
	}

	/* 'info' [R] */
	if (info_fops) {
		entry = create_proc_entry(ACPI_SBS_FILE_INFO, S_IRUGO, *dir);
		if (!entry) {
881 882
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"create_proc_entry() failed"));
883 884 885 886 887 888 889 890 891 892 893
		} else {
			entry->proc_fops = info_fops;
			entry->data = data;
			entry->owner = THIS_MODULE;
		}
	}

	/* 'state' [R] */
	if (state_fops) {
		entry = create_proc_entry(ACPI_SBS_FILE_STATE, S_IRUGO, *dir);
		if (!entry) {
894 895
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"create_proc_entry() failed"));
896 897 898 899 900 901 902 903 904 905 906
		} else {
			entry->proc_fops = state_fops;
			entry->data = data;
			entry->owner = THIS_MODULE;
		}
	}

	/* 'alarm' [R/W] */
	if (alarm_fops) {
		entry = create_proc_entry(ACPI_SBS_FILE_ALARM, S_IRUGO, *dir);
		if (!entry) {
907 908
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"create_proc_entry() failed"));
909 910 911 912 913 914 915
		} else {
			entry->proc_fops = alarm_fops;
			entry->data = data;
			entry->owner = THIS_MODULE;
		}
	}

916
	return 0;
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
}

static void
acpi_sbs_generic_remove_fs(struct proc_dir_entry **dir,
			   struct proc_dir_entry *parent_dir)
{

	if (*dir) {
		remove_proc_entry(ACPI_SBS_FILE_INFO, *dir);
		remove_proc_entry(ACPI_SBS_FILE_STATE, *dir);
		remove_proc_entry(ACPI_SBS_FILE_ALARM, *dir);
		remove_proc_entry((*dir)->name, parent_dir);
		*dir = NULL;
	}

}

/* Smart Battery Interface */

static struct proc_dir_entry *acpi_battery_dir = NULL;

static int acpi_battery_read_info(struct seq_file *seq, void *offset)
{
940
	struct acpi_battery *battery = seq->private;
941
	struct acpi_sbs *sbs = battery->sbs;
942 943 944
	int cscale;
	int result = 0;

945
	if (sbs_mutex_lock(sbs)) {
946
		return -ENODEV;
947 948
	}

949 950 951
	result = acpi_check_update_proc(sbs);
	if (result)
		goto end;
952

953 954
	if (update_time == 0) {
		result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_INFO);
955
		if (result) {
956 957
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"acpi_sbs_update_run() failed"));
958 959 960
		}
	}

961
	if (battery->battery_present) {
962 963 964 965 966 967 968 969 970 971 972
		seq_printf(seq, "present:                 yes\n");
	} else {
		seq_printf(seq, "present:                 no\n");
		goto end;
	}

	if (battery->info.capacity_mode) {
		cscale = battery->info.vscale * battery->info.ipscale;
	} else {
		cscale = battery->info.ipscale;
	}
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 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004

	seq_printf(seq, "battery technology:      rechargeable\n");

	seq_printf(seq, "design voltage:          %i mV\n",
		   battery->info.design_voltage * battery->info.vscale);

	seq_printf(seq, "design capacity warning: unknown\n");
	seq_printf(seq, "design capacity low:     unknown\n");
	seq_printf(seq, "capacity granularity 1:  unknown\n");
	seq_printf(seq, "capacity granularity 2:  unknown\n");

	seq_printf(seq, "model number:            %s\n",
		   battery->info.device_name);

	seq_printf(seq, "serial number:           %i\n",
		   battery->info.serial_number);

	seq_printf(seq, "battery type:            %s\n",
		   battery->info.device_chemistry);

	seq_printf(seq, "OEM info:                %s\n",
		   battery->info.manufacturer_name);

      end:

1005
	sbs_mutex_unlock(sbs);
1006

1007
	return result;
1008 1009 1010 1011 1012 1013 1014 1015 1016
}

static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
{
	return single_open(file, acpi_battery_read_info, PDE(inode)->data);
}

static int acpi_battery_read_state(struct seq_file *seq, void *offset)
{
1017 1018
	struct acpi_battery *battery = seq->private;
	struct acpi_sbs *sbs = battery->sbs;
1019 1020 1021 1022
	int result = 0;
	int cscale;
	int foo;

1023
	if (sbs_mutex_lock(sbs)) {
1024
		return -ENODEV;
1025 1026
	}

1027 1028 1029
	result = acpi_check_update_proc(sbs);
	if (result)
		goto end;
1030

1031 1032
	if (update_time == 0) {
		result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_STATE);
1033
		if (result) {
1034 1035
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"acpi_sbs_update_run() failed"));
1036 1037 1038
		}
	}

1039
	if (battery->battery_present) {
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
		seq_printf(seq, "present:                 yes\n");
	} else {
		seq_printf(seq, "present:                 no\n");
		goto end;
	}

	if (battery->info.capacity_mode) {
		cscale = battery->info.vscale * battery->info.ipscale;
	} else {
		cscale = battery->info.ipscale;
	}

1052
	if (battery->state.battery_state & 0x0010) {
1053 1054 1055 1056
		seq_printf(seq, "capacity state:          critical\n");
	} else {
		seq_printf(seq, "capacity state:          ok\n");
	}
V
Vladimir Lebedev 已提交
1057 1058 1059 1060 1061

	foo = (s16) battery->state.amperage * battery->info.ipscale;
	if (battery->info.capacity_mode) {
		foo = foo * battery->info.design_voltage / 1000;
	}
1062 1063
	if (battery->state.amperage < 0) {
		seq_printf(seq, "charging state:          discharging\n");
V
Vladimir Lebedev 已提交
1064 1065
		seq_printf(seq, "present rate:            %d %s\n",
			   -foo, battery->info.capacity_mode ? "mW" : "mA");
1066 1067
	} else if (battery->state.amperage > 0) {
		seq_printf(seq, "charging state:          charging\n");
V
Vladimir Lebedev 已提交
1068 1069
		seq_printf(seq, "present rate:            %d %s\n",
			   foo, battery->info.capacity_mode ? "mW" : "mA");
1070 1071 1072 1073 1074 1075
	} else {
		seq_printf(seq, "charging state:          charged\n");
		seq_printf(seq, "present rate:            0 %s\n",
			   battery->info.capacity_mode ? "mW" : "mA");
	}

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 1081 1082 1083 1084

	seq_printf(seq, "present voltage:         %i mV\n",
		   battery->state.voltage * battery->info.vscale);

      end:

1085
	sbs_mutex_unlock(sbs);
1086

1087
	return result;
1088 1089 1090 1091 1092 1093 1094 1095 1096
}

static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
{
	return single_open(file, acpi_battery_read_state, PDE(inode)->data);
}

static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
{
1097
	struct acpi_battery *battery = seq->private;
1098
	struct acpi_sbs *sbs = battery->sbs;
1099 1100 1101
	int result = 0;
	int cscale;

1102
	if (sbs_mutex_lock(sbs)) {
1103
		return -ENODEV;
1104 1105
	}

1106 1107 1108
	result = acpi_check_update_proc(sbs);
	if (result)
		goto end;
1109

1110 1111
	if (update_time == 0) {
		result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_ALARM);
1112
		if (result) {
1113 1114
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"acpi_sbs_update_run() failed"));
1115 1116 1117
		}
	}

1118
	if (!battery->battery_present) {
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
		seq_printf(seq, "present:                 no\n");
		goto end;
	}

	if (battery->info.capacity_mode) {
		cscale = battery->info.vscale * battery->info.ipscale;
	} else {
		cscale = battery->info.ipscale;
	}

	seq_printf(seq, "alarm:                   ");
	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 1135 1136 1137 1138 1139
	} else {
		seq_printf(seq, "disabled\n");
	}

      end:

1140
	sbs_mutex_unlock(sbs);
1141

1142
	return result;
1143 1144 1145 1146 1147 1148
}

static ssize_t
acpi_battery_write_alarm(struct file *file, const char __user * buffer,
			 size_t count, loff_t * ppos)
{
1149 1150
	struct seq_file *seq = file->private_data;
	struct acpi_battery *battery = seq->private;
1151
	struct acpi_sbs *sbs = battery->sbs;
1152 1153 1154
	char alarm_string[12] = { '\0' };
	int result, old_alarm, new_alarm;

1155
	if (sbs_mutex_lock(sbs)) {
1156
		return -ENODEV;
1157 1158
	}

1159 1160 1161
	result = acpi_check_update_proc(sbs);
	if (result)
		goto end;
1162

1163
	if (!battery->battery_present) {
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
		result = -ENODEV;
		goto end;
	}

	if (count > sizeof(alarm_string) - 1) {
		result = -EINVAL;
		goto end;
	}

	if (copy_from_user(alarm_string, buffer, count)) {
		result = -EFAULT;
		goto end;
	}

	alarm_string[count] = 0;

	old_alarm = battery->alarm.remaining_capacity;
	new_alarm = simple_strtoul(alarm_string, NULL, 0);

	result = acpi_battery_set_alarm(battery, new_alarm);
	if (result) {
1185 1186
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_battery_set_alarm() failed"));
1187
		acpi_battery_set_alarm(battery, old_alarm);
1188 1189 1190 1191
		goto end;
	}
	result = acpi_battery_get_alarm(battery);
	if (result) {
1192 1193
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_battery_get_alarm() failed"));
1194
		acpi_battery_set_alarm(battery, old_alarm);
1195 1196 1197 1198
		goto end;
	}

      end:
1199
	sbs_mutex_unlock(sbs);
1200 1201

	if (result) {
1202
		return result;
1203
	} else {
1204
		return count;
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
	}
}

static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
{
	return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
}

static struct file_operations acpi_battery_info_fops = {
	.open = acpi_battery_info_open_fs,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static struct file_operations acpi_battery_state_fops = {
	.open = acpi_battery_state_open_fs,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static struct file_operations acpi_battery_alarm_fops = {
	.open = acpi_battery_alarm_open_fs,
	.read = seq_read,
	.write = acpi_battery_write_alarm,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

/* Legacy AC Adapter Interface */

static struct proc_dir_entry *acpi_ac_dir = NULL;

static int acpi_ac_read_state(struct seq_file *seq, void *offset)
{
1244
	struct acpi_sbs *sbs = seq->private;
1245 1246
	int result;

1247
	if (sbs_mutex_lock(sbs)) {
1248
		return -ENODEV;
1249 1250
	}

1251 1252
	if (update_time == 0) {
		result = acpi_sbs_update_run(sbs, -1, DATA_TYPE_AC_STATE);
1253
		if (result) {
1254 1255
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"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 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
}

static int acpi_ac_state_open_fs(struct inode *inode, struct file *file)
{
	return single_open(file, acpi_ac_read_state, PDE(inode)->data);
}

static struct file_operations acpi_ac_state_fops = {
	.open = acpi_ac_state_open_fs,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

/* --------------------------------------------------------------------------
                                 Driver Interface
   -------------------------------------------------------------------------- */

/* Smart Battery */

static int acpi_battery_add(struct acpi_sbs *sbs, int id)
{
	int is_present;
	int result;
	char dir_name[32];
	struct acpi_battery *battery;

	battery = &sbs->battery[id];

	battery->alive = 0;

	battery->init_state = 0;
	battery->id = id;
	battery->sbs = sbs;

	result = acpi_battery_select(battery);
	if (result) {
1303 1304
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_battery_select() failed"));
1305 1306 1307 1308 1309
		goto end;
	}

	result = acpi_battery_get_present(battery);
	if (result) {
1310 1311
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_battery_get_present() failed"));
1312 1313 1314
		goto end;
	}

1315
	is_present = battery->battery_present;
1316 1317 1318 1319

	if (is_present) {
		result = acpi_battery_init(battery);
		if (result) {
1320 1321
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"acpi_battery_init() failed"));
1322 1323 1324 1325 1326
			goto end;
		}
		battery->init_state = 1;
	}

1327
	sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1328 1329 1330 1331 1332 1333 1334 1335

	result = acpi_sbs_generic_add_fs(&battery->battery_entry,
					 acpi_battery_dir,
					 dir_name,
					 &acpi_battery_info_fops,
					 &acpi_battery_state_fops,
					 &acpi_battery_alarm_fops, battery);
	if (result) {
1336 1337
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_generic_add_fs() failed"));
1338 1339 1340 1341
		goto end;
	}
	battery->alive = 1;

1342 1343 1344 1345
	printk(KERN_INFO PREFIX "%s [%s]: Battery Slot [%s] (battery %s)\n",
	       ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device), dir_name,
	       sbs->battery->battery_present ? "present" : "absent");

1346
      end:
1347
	return result;
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
}

static void acpi_battery_remove(struct acpi_sbs *sbs, int id)
{

	if (sbs->battery[id].battery_entry) {
		acpi_sbs_generic_remove_fs(&(sbs->battery[id].battery_entry),
					   acpi_battery_dir);
	}
}

static int acpi_ac_add(struct acpi_sbs *sbs)
{
	int result;

	result = acpi_ac_get_present(sbs);
	if (result) {
1365 1366
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_ac_get_present() failed"));
1367 1368 1369 1370 1371 1372 1373 1374
		goto end;
	}

	result = acpi_sbs_generic_add_fs(&sbs->ac_entry,
					 acpi_ac_dir,
					 ACPI_AC_DIR_NAME,
					 NULL, &acpi_ac_state_fops, NULL, sbs);
	if (result) {
1375 1376
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_generic_add_fs() failed"));
1377 1378 1379
		goto end;
	}

1380 1381 1382 1383
	printk(KERN_INFO PREFIX "%s [%s]: AC Adapter [%s] (%s)\n",
	       ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device),
	       ACPI_AC_DIR_NAME, sbs->ac.ac_present ? "on-line" : "off-line");

1384 1385
      end:

1386
	return result;
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
}

static void acpi_ac_remove(struct acpi_sbs *sbs)
{

	if (sbs->ac_entry) {
		acpi_sbs_generic_remove_fs(&sbs->ac_entry, acpi_ac_dir);
	}
}

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 1406 1407 1408 1409 1410
	int result = 0, cnt;
	int old_ac_present = -1;
	int old_battery_present = -1;
	int new_ac_present = -1;
	int new_battery_present = -1;
	int id_min = 0, id_max = MAX_SBS_BAT - 1;
1411
	char dir_name[32];
1412 1413 1414 1415 1416 1417 1418 1419
	int do_battery_init = 0, do_ac_init = 0;
	int old_remaining_capacity = 0;
	int update_ac = 1, update_battery = 1;
	int up_tm = update_time;

	if (sbs_zombie(sbs)) {
		goto end;
	}
1420

1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
	if (id >= 0) {
		id_min = id_max = id;
	}

	if (data_type == DATA_TYPE_COMMON && up_tm > 0) {
		cnt = up_tm / (up_tm > UPDATE_DELAY ? UPDATE_DELAY : up_tm);
		if (sbs->run_cnt % cnt != 0) {
			update_battery = 0;
		}
	}

	sbs->run_cnt++;

	if (!update_ac && !update_battery) {
1435 1436 1437
		goto end;
	}

1438
	old_ac_present = sbs->ac.ac_present;
1439 1440 1441

	result = acpi_ac_get_present(sbs);
	if (result) {
1442 1443
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"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 1450 1451
	if (sbs->run_cnt == 1 && data_type == DATA_TYPE_COMMON) {
		do_ac_init = 1;
	}
1452

1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
	if (do_ac_init) {
		result = acpi_sbs_generate_event(sbs->device,
						 ACPI_SBS_AC_NOTIFY_STATUS,
						 new_ac_present,
						 ACPI_AC_DIR_NAME,
						 ACPI_AC_CLASS);
		if (result) {
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"acpi_sbs_generate_event() failed"));
		}
	}

	if (data_type == DATA_TYPE_COMMON) {
		if (!do_ac_init && !update_battery) {
			goto end;
		}
	}

	if (data_type == DATA_TYPE_AC_STATE && !do_ac_init) {
1472 1473 1474
		goto end;
	}

1475
	for (id = id_min; id <= id_max; id++) {
1476 1477 1478 1479 1480 1481 1482
		battery = &sbs->battery[id];
		if (battery->alive == 0) {
			continue;
		}

		old_remaining_capacity = battery->state.remaining_capacity;

1483
		old_battery_present = battery->battery_present;
1484 1485 1486

		result = acpi_battery_select(battery);
		if (result) {
1487 1488
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"acpi_battery_select() failed"));
1489 1490 1491 1492
		}

		result = acpi_battery_get_present(battery);
		if (result) {
1493 1494
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"acpi_battery_get_present() failed"));
1495 1496
		}

1497
		new_battery_present = battery->battery_present;
1498 1499 1500

		do_battery_init = ((old_battery_present != new_battery_present)
				   && new_battery_present);
1501 1502 1503
		if (!new_battery_present)
			goto event;
		if (do_ac_init || do_battery_init) {
1504 1505
			result = acpi_battery_init(battery);
			if (result) {
1506 1507 1508
				ACPI_EXCEPTION((AE_INFO, AE_ERROR,
						"acpi_battery_init() "
						"failed"));
1509 1510
			}
		}
1511
		if (sbs_zombie(sbs)) {
1512 1513
			goto end;
		}
1514 1515 1516 1517 1518

		if ((data_type == DATA_TYPE_COMMON
		     || data_type == DATA_TYPE_INFO)
		    && new_battery_present) {
			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 1525 1526 1527 1528 1529
		}
		if (data_type == DATA_TYPE_INFO) {
			continue;
		}
		if (sbs_zombie(sbs)) {
			goto end;
		}
1530

1531 1532 1533
		if ((data_type == DATA_TYPE_COMMON
		     || data_type == DATA_TYPE_STATE)
		    && new_battery_present) {
1534 1535
			result = acpi_battery_get_state(battery);
			if (result) {
1536
				ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1537
						"acpi_battery_get_state() failed"));
1538 1539
			}
		}
1540 1541
		if (data_type == DATA_TYPE_STATE) {
			goto event;
1542
		}
1543 1544
		if (sbs_zombie(sbs)) {
			goto end;
1545 1546
		}

1547 1548 1549 1550
		if ((data_type == DATA_TYPE_COMMON
		     || data_type == DATA_TYPE_ALARM)
		    && new_battery_present) {
			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 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
		if (data_type == DATA_TYPE_ALARM) {
			continue;
		}
		if (sbs_zombie(sbs)) {
			goto end;
		}

	      event:

		if (old_battery_present != new_battery_present || do_ac_init ||
		    old_remaining_capacity !=
		    battery->state.remaining_capacity) {
1569
			sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1570 1571 1572 1573 1574 1575
			result = acpi_sbs_generate_event(sbs->device,
							 ACPI_SBS_BATTERY_NOTIFY_STATUS,
							 new_battery_present,
							 dir_name,
							 ACPI_BATTERY_CLASS);
			if (result) {
1576
				ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1577 1578
						"acpi_sbs_generate_event() "
						"failed"));
1579 1580 1581 1582 1583
			}
		}
	}

      end:
1584

1585
	return result;
1586 1587
}

1588
static void acpi_sbs_update_time(void *data)
1589 1590 1591 1592
{
	struct acpi_sbs *sbs = data;
	unsigned long delay = -1;
	int result;
1593
	unsigned int up_tm = update_time;
1594

1595 1596
	if (sbs_mutex_lock(sbs))
		return;
1597

1598
	result = acpi_sbs_update_run(sbs, -1, DATA_TYPE_COMMON);
1599
	if (result) {
1600 1601
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_update_run() failed"));
1602 1603
	}

1604
	if (sbs_zombie(sbs)) {
1605 1606 1607
		goto end;
	}

1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621
	if (!up_tm) {
		if (timer_pending(&sbs->update_timer))
			del_timer(&sbs->update_timer);
	} else {
		delay = (up_tm > UPDATE_DELAY ? UPDATE_DELAY : up_tm);
		delay = jiffies + HZ * delay;
		if (timer_pending(&sbs->update_timer)) {
			mod_timer(&sbs->update_timer, delay);
		} else {
			sbs->update_timer.data = (unsigned long)data;
			sbs->update_timer.function = acpi_sbs_update_time_run;
			sbs->update_timer.expires = delay;
			add_timer(&sbs->update_timer);
		}
1622 1623 1624
	}

      end:
1625 1626

	sbs_mutex_unlock(sbs);
1627 1628 1629 1630 1631
}

static int acpi_sbs_add(struct acpi_device *device)
{
	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 1637 1638 1639 1640
	unsigned long val;

	status =
	    acpi_evaluate_integer(device->parent->handle, "_EC", NULL, &val);
	if (ACPI_FAILURE(status)) {
1641
		ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Error obtaining _EC"));
1642 1643
		return -EIO;
	}
1644

1645
	sbs = kzalloc(sizeof(struct acpi_sbs), GFP_KERNEL);
1646
	if (!sbs) {
1647 1648 1649
		ACPI_EXCEPTION((AE_INFO, AE_ERROR, "kzalloc() failed"));
		result = -ENOMEM;
		goto end;
1650 1651
	}

1652 1653 1654 1655 1656
	mutex_init(&sbs->mutex);

	sbs_mutex_lock(sbs);

	sbs->base = (val & 0xff00ull) >> 8;
1657 1658 1659 1660 1661 1662 1663 1664
	sbs->device = device;

	strcpy(acpi_device_name(device), ACPI_SBS_DEVICE_NAME);
	strcpy(acpi_device_class(device), ACPI_SBS_CLASS);
	acpi_driver_data(device) = sbs;

	result = acpi_ac_add(sbs);
	if (result) {
1665
		ACPI_EXCEPTION((AE_INFO, AE_ERROR, "acpi_ac_add() failed"));
1666 1667
		goto end;
	}
1668 1669 1670
	status = acpi_evaluate_integer(device->handle, "_SBS", NULL, &sbs_obj);
	if (status) {
		ACPI_EXCEPTION((AE_INFO, status,
1671
				"acpi_evaluate_integer() failed"));
1672 1673 1674 1675 1676 1677
		result = -EIO;
		goto end;
	}
	if (sbs_obj > 0) {
		result = acpi_sbsm_get_info(sbs);
		if (result) {
1678 1679
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"acpi_sbsm_get_info() failed"));
1680 1681 1682 1683
			goto end;
		}
		sbs->sbsm_present = 1;
	}
1684

1685 1686 1687
	if (sbs->sbsm_present == 0) {
		result = acpi_battery_add(sbs, 0);
		if (result) {
1688 1689
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"acpi_battery_add() failed"));
1690 1691 1692 1693 1694 1695 1696
			goto end;
		}
	} else {
		for (id = 0; id < MAX_SBS_BAT; id++) {
			if ((sbs->sbsm_batteries_supported & (1 << id))) {
				result = acpi_battery_add(sbs, id);
				if (result) {
1697
					ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1698
							"acpi_battery_add() failed"));
1699 1700 1701 1702 1703 1704 1705 1706 1707
					goto end;
				}
			}
		}
	}

	sbs->handle = device->handle;

	init_timer(&sbs->update_timer);
1708 1709 1710
	result = acpi_check_update_proc(sbs);
	if (result)
		goto end;
1711 1712

      end:
1713 1714 1715

	sbs_mutex_unlock(sbs);

1716
	if (result) {
1717 1718 1719 1720 1721
		remove_result = acpi_sbs_remove(device, 0);
		if (remove_result) {
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"acpi_sbs_remove() failed"));
		}
1722 1723
	}

1724
	return result;
1725 1726
}

1727
static int acpi_sbs_remove(struct acpi_device *device, int type)
1728
{
L
Len Brown 已提交
1729
	struct acpi_sbs *sbs;
1730 1731
	int id;

1732 1733 1734 1735
	if (!device) {
		return -EINVAL;
	}

1736
	sbs = acpi_driver_data(device);
1737
	if (!sbs) {
1738
		return -EINVAL;
1739 1740
	}

1741 1742
	sbs_mutex_lock(sbs);

1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753
	sbs->zombie = 1;
	del_timer_sync(&sbs->update_timer);
	acpi_os_wait_events_complete(NULL);
	del_timer_sync(&sbs->update_timer);

	for (id = 0; id < MAX_SBS_BAT; id++) {
		acpi_battery_remove(sbs, id);
	}

	acpi_ac_remove(sbs);

1754 1755 1756
	sbs_mutex_unlock(sbs);

	mutex_destroy(&sbs->mutex);
1757

1758 1759
	kfree(sbs);

1760
	return 0;
1761 1762
}

1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788
static void acpi_sbs_rmdirs(void)
{
	if (acpi_ac_dir) {
		acpi_unlock_ac_dir(acpi_ac_dir);
		acpi_ac_dir = NULL;
	}
	if (acpi_battery_dir) {
		acpi_unlock_battery_dir(acpi_battery_dir);
		acpi_battery_dir = NULL;
	}
}

static int acpi_sbs_resume(struct acpi_device *device)
{
	struct acpi_sbs *sbs;

	if (!device)
		return -EINVAL;

	sbs = device->driver_data;

	sbs->run_cnt = 0;

	return 0;
}

1789 1790 1791 1792
static int __init acpi_sbs_init(void)
{
	int result = 0;

1793 1794 1795
	if (acpi_disabled)
		return -ENODEV;

1796 1797 1798
	if (capacity_mode != DEF_CAPACITY_UNIT
	    && capacity_mode != MAH_CAPACITY_UNIT
	    && 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 1805
	}

	acpi_ac_dir = acpi_lock_ac_dir();
	if (!acpi_ac_dir) {
1806 1807
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_lock_ac_dir() failed"));
1808
		return -ENODEV;
1809 1810 1811 1812
	}

	acpi_battery_dir = acpi_lock_battery_dir();
	if (!acpi_battery_dir) {
1813 1814
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_lock_battery_dir() failed"));
1815
		acpi_sbs_rmdirs();
1816
		return -ENODEV;
1817 1818 1819 1820
	}

	result = acpi_bus_register_driver(&acpi_sbs_driver);
	if (result < 0) {
1821 1822
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_bus_register_driver() failed"));
1823
		acpi_sbs_rmdirs();
1824
		return -ENODEV;
1825 1826
	}

1827
	return 0;
1828 1829 1830 1831 1832 1833
}

static void __exit acpi_sbs_exit(void)
{
	acpi_bus_unregister_driver(&acpi_sbs_driver);

1834
	acpi_sbs_rmdirs();
1835

1836
	return;
1837 1838 1839 1840
}

module_init(acpi_sbs_init);
module_exit(acpi_sbs_exit);